MZ Get TileData of different map.

I tried looking, but I'm unable to find what I'm looking for, and am struggling to figure this out.

Basically, I have a plugin, that lets me replace/swap an existing tile on the current map, with a specified tileId from the tileset currently being used at the location of X,Y,Z where Z is the layer, and this works fine.
Here is the code to replace tiles: $dataMap[[(z * $dataMap.height + y) * $dataMap.width + x]] = tileId
Currently, I store the X,Y,Z and TileId for replacement in an array.. 1 by 1. This is quite tedious.

But then I remembered, RPG Maker MZ has a nice little command: $gameMap.tileId(x, y, z) which grabs the tileId at X,Y,Z. Unfortunately it only grabs the tileId of the current map.

And that's where I thought, why don't I just grab the data of a different map, store that into an array. And then combine it to replace tiles en masse using a loop. If I were to create prefabbed buildings, structures, etc.. on a secondary map, I could just specify a start x & y, and a finish x & y in a nested loop, to grab that data, store it into an array, and then do basically the same thing in reverse to place them on the map currently being played on.

And that's where I'm now stuck. I have absolutely no idea how to pull the tiledata of a different map.
I tried: DataManager.loadMapData(mapId). This sort of worked. It did the following:
Loaded the map data, so that using $dataMap.data would give me data of the 'loaded' map, but with a caveat. Trying to use the replacement code to swap the tiles didn't work.

So, I tried, using 'loadMapData' (abbreviated for explanations sake) to first load the secondary map, grab the data, store it into an array, and then use 'loadMapData' again to reload the primary map and then try to swap the tiles. Unfortunately, this also didn't work. There were no errors, just the map visually didn't update.

But after restarting the game, and swapping the tiles with my 'now' known data and loop, it did work. So I then tried to refresh or update the map, refresh or update the scene. But again, nothing. My only guess is 'loadMapData' does something that just messes with things.

So I'm here asking for help, in hopes someone has the answers I'm looking for. I 'could' do this manually by just building the maps, loading the json file in VS-Code, copy & pasting the array into a new plugin file, and writing a function that will do this. But I feel like I'm 'close' to having this done within game, allowing me to use common events and such. Would there be a difference between doing it in game vs writing a function as a plugin? Nope. They'd both do the same thing in the end. I just want to know how to do it in the engine to learn and see why/what is going on with what I tried already.

Thank you for your time, sorry it's a long post, brain is melted and running on fumes lol.
I think I understand what you're doing. I've messed around with something similar, but rather than getting the tile data I was trying to get the meta data.

Tile data, Meta data, Event data...it's all in the Map object. And the system only works with 1 Map object at a time; that's $dataMap.
All loadMapData() does is rewrite the $dataMap object with data from another map###.json file.

Working with that alone would mean loading 1 map object just to store the data you want to a variable, then reloading the previous map to make changes with the new data.

It might be easier to just create a second map object, especially if it's just 1 map in particular.
DataManager.loadDataFile("$dataMap2", "Map002.json")

So then it's just
$dataMap.data[ *some portion* ] = $dataMap2.data [ *some portion* ]
Assuming the 2 maps use the same tilesets.
Hmm.. When I get time tomorrow, I'll try it out. Thanks for your input. It looks promising.

Edit: And that works really well. Thank you for that.
I knew I had to have missed something going through the files. I was contemplating using an XMLHttpRequest, or require() using node.js fetch api to grab the file directly through a script, but as I'm still in the early stages of development I didn't want to commit to something that would trap me in how the game is deployed. Especially if a method such as this exists. Again, thank you.
Not trying to bumb, but this fits in the same scope of the original question, so no reason to make a new thread.

Okay, I'm having a slightly new issue. Here is my current code:
Spoiler: Code
Load Data Function
JavaScript:
    Game_Map.prototype.loadMapData = function () {
        let mapName = "";
        let mapCount = $dataMapInfos.length;
        let mapData = [];
        // Skip to 2. | 0 = Null | 1 = BaseMap
        for (let i = 2; i < mapCount; i++) {
            mapName = i < 10? "Map00" + i : i < 100 ? "Map0" + i : "Map" + i;
            mapName += ".json";
            DataManager.loadDataFile(`$dataMap${i}`, mapName);
        }
          return;
       
        }

Tile Replacement Function
Code:
        // Function to create a room
        // x = x position of room
        // y = y position of room
        // z = layer of room (0,1,2,3)
        Game_Map.prototype.createRoom = function (mapId, x, y, z) {
          mapId = mapId | 0;
          x = x | 0;
          y = y | 0;
          z = z | 0;
          z = z.clamp(0, 3);
          if (mapId < 2) return; // | 0 = Null | 1 = BaseMap | No point in loading the base map into the base map. And null would give errors.
          let mapData = eval(`$dataMap${mapId}`);
        for(let i = 0; i < mapData.height; i++) {
            for(let j = 0; j < mapData.width; j++){
              // Set tile data
              // Places the tile data from $dataMap${id} into $dataMap at the specified x,y,z
              // Note - This copies the entire map of $dataMap${id} into $dataMap at the specified x,y,z. If you're not careful overlapping maps can cause issues.
                $dataMap.data[[(z * $dataMap.height + (y + i)) * $dataMap.width + (x + j)]] = mapData.data[[(z * mapData.height + i) * mapData.width + j]]
            }
        }
You'll notice I have 2 separate functions. One to load the data (doesn't need to be in Game_Map, I just wrote it this way and haven't changed it yet) and another to actually process the data. There is a 3rd function, but it's simply a call to refresh/update and save the map so reloading the map from transfering in/out keeps it saved. (3rd function works fine).

The problem I'm having is that I cannot combine these 2 functions. More specifically, if I try to use any of the global $dataMap{id} variables in any sort of way, within the same function immediately after creating them, I get thrown null errors. Which tells me it didn't fully load before execution. Which honestly is kind of weird. I noticed this same issue when trying to use them both within a common event triggered to load at start of new game and/or when attempting to do the same from an event by manually clicking on it.

However, by breaking them apart and putting a wait 1ms command in the event, there are no issues. This is fine for my current use case. I'm just wondering if this will cause problems down the line, and if so how I can fix it preemptively.

Spoiler: Reason for
For those curious on how/what this is being used for in case this information helps. I have a 4th function that runs on 'New Game' that creates an array of objects (object array?) that basically creates for me data that can be used to make a roguelike map of predetermined shapes, variations, etc.. using the Depth-First search algorithm to ensure the rooms are connected.

The 'loadData' and 'replacement' functions are created with this in mind. I create child maps that hold prefabs. Load that data, and using a common event handle that data based on the roguelike map created to replace tiles on the parent map as needed with the base map. For now the child maps are base 19x19, meaning a base map of 256x256 is needed to allow for a decent amount of rooms.

Original thread: https://forums.rpgmakerweb.com/threads/get-tiledata-of-different-map.154417/