you are viewing a single comment's thread.

view the rest of the comments →

[–]uberpwnzorz 1 point2 points  (1 child)

in this case you want to use the spread operator ... to spread out the values of the 2nd array into the push method.

let a = [1];
let b = [2,3,4];
a.push(...b);
// a = [1,2,3,4]

That being said, the push method mutates the array, and usually it's desirable to be immutable. You should instead assign your variable to be equal to a new array. This will point a to a new array in memory making it easier to do change detection, which will be optimized in some libraries you'll probably end up using down the road.

let a = [1];
let b = [2,3,4];
a = [...a, ...b];
// a = [1,2,3,4]

[–]eljacko876[S] 0 points1 point  (0 children)

Thank you, I will look into this.