Adding the content of 1 array to another

I have a set of arrays, which al contain ID's of scenes. Each array has its own set of ID's stored in them.
Now I need to copy the content of 1 array into that of another array.

Before:
Code:
Variable 59 = [5901, 5902, 5903]
Variable 60 = [6001, 6002, 6003]

After:
Code:
Variable 59 = [5901, 5902, 5903, 6001, 6002, 6003]
Variable 60 = [6001, 6002, 6003]

I'm not too great with javascript, but but I tried to do the following:
Code:
$gameVariables.value(59).push($gameVariables.value(60));

This indeed resulted in: Variable 59 = [5901, 5902, 5903, 6001, 6002, 6003]
However... 6001, 6002, 6003 is 1 entry, instead of 3 individual entries.

So when I try to pick a random ID from the array, i can get "5901" or "5902" or "5903" or "6001, 6002, 6003" but I won't get "6001" or "6002" or "6003"

How do I add the content from array in variable 60 into the array in variable 59 without it all being a single entry?
Try:
Code:
$gameVariables.value(60).forEach(element => $gameVariables.value(59).push(element))

Edit: Apparently, you can also do:
Code:
$gameVariables.setValue(59, $gameVariables.value(59).concat($gameVariables.value(60)))

There's probably a degree of improved performance on the second one, but it would be negligible for your use case anyway.
Try:
Code:
$gameVariables.value(60).forEach(element => $gameVariables.value(59).push(element))

Edit: Apparently, you can also do:
Code:
$gameVariables.setValue(59, $gameVariables.value(59).concat($gameVariables.value(60)))

There's probably a degree of improved performance on the second one, but it would be negligible for your use case anyway.
Yes, this seems to work! Thanks a lot, you're a life sa... Okay maybe not a life saver. A project saver! 😛

Oh, I didn't see the edit yet... Well, the arrays might be a looooooot longer in the end then 3 entries per array. So, if that would increase the performance issues, then the other code might be best. I'll try both of them with longer arrays.
Well, the arrays might be a looooooot longer in the end then 3 entries per array. So, if that would increase the performance issues, then the other code might be best.
Certainly try both. But when people get into measuring performance of iterating through arrays like that, it generally will start to make a difference if you're talking about thousands of operations, which is probably still not what you mean by a loooooooooooot longer.
Well, thousands is a lot, but I can only hope.

The idea is that I make a open world, where random scenes can be triggered depending on what kind of area you are in. Snow wolves attack in snowy area's and desert cats can be caught in sandy area's. This counts for enemies, but also random NPC's, treasure/loot, etc etc. When you pic "Walk north" you never know if you walk into an enemy or a friend.

I have 59 different area types so far (like sea, beach, grass, snow, forest etc, but also farms for example. Also each city has their own area type)

So I have scenes that can trigger in snow, I have scenes that trigger in the forest, etc etc. But I also have scenes that can trigger wherever (outside the city), like skeletons or dragons, and those are stored in the array for area type 60.

I do intent to add many (and when I say many, I mean many) scenes to the game to avoid player keep getting the same events over and over before the main story is complete. So thousands might be a bit enthusiastic, but honestly I would love to have 1000+ scenes that can spawn everywhere. (And those that can spawn everywhere are the ones being copied over into the other arrays.)
You can also use the spread operator for this, much cleaner and more efficient than a forEach loop:

$gameVariables.value(59).push(...$gameVariables.value(60))
You can also use the spread operator for this, much cleaner and more efficient than a forEach loop:

$gameVariables.value(59).push(...$gameVariables.value(60))
Oh wow! Works smoothly! You've only add ... to the script call I already had, right? Can you tell me what the ... does? For future use in different script calls.
Oh wow! Works smoothly! You've only add ... to the script call I already had, right? Can you tell me what the ... does? For future use in different script calls.
It basically expands an array into individual values when passed to a parameter that takes multiple arguments.

So if you have

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]

and do

arr1.push(...arr2)

arr1 will now be [1, 2, 3, 4, 5, 6]

It's as if you wrote

arr1.push(arr2[0], arr2[1], arr2[2])

Note that when working directly with arrays, you can also do something like

arr1 = [...arr1, ...arr2]

and get the same result.
Thanks. This was a huge issue for my project War and Wonder. Being able to add the one array into other arrays makes that I do not have to add all the possible ID's manually. Which, knowing I have 59 of those arrays, would be a huge pain in the rear.
Thanks. This was a huge issue for my project War and Wonder. Being able to add the one array into other arrays makes that I do not have to add all the possible ID's manually. Which, knowing I have 59 of those arrays, would be a huge pain in the rear.
Yeah, ES6 has given us some really badass stuff for making life easier. I'm currently writing a tutorial topic about all the new features people might not know about yet. (there is one from 2016 but it's broken with all the forum tag changes)
or you can try:
let merge = [].concat(arr1, arr2); can work too and call it with merge[1]
or anything or throw it into a variable that way.

there are different methodes as I use this to merge the array to read
them nicely:

let merge = [].concat.apply([], array); (as array take it from 1 another array
to read something else, I combine them to 1.
or you can try:
let merge = [].concat(arr1, arr2); can work too and call it with merge[1]
or anything or throw it into a variable that way.

there are different methodes as I use this to merge the array to read
them nicely:

let merge = [].concat.apply([], array); (as array take it from 1 another array
to read something else, I combine them to 1.
That is getting a little above my understanding of javascript. 😛

But I do not need to combine them really, I do need to copy var 60 into the other arrays, but I do need the array in 60 in tact.

The scipt call Trihan gave me was perfect for this. And in that line of code, I understood how the code works.

Original thread: https://forums.rpgmakerweb.com/threads/adding-the-content-of-1-array-to-another.151991/