I have got code like this:
arr1 = [1,2,3];
arr2 = [4,5,6];
I want to add array 2 to array 1 so arr1 will equal [1,2,3,4,5,6]
.
I have tried arr1.push(arr2)
, but that returns [1,2,3,[4,5,6]]
.
I have got code like this:
arr1 = [1,2,3];
arr2 = [4,5,6];
I want to add array 2 to array 1 so arr1 will equal [1,2,3,4,5,6]
.
I have tried arr1.push(arr2)
, but that returns [1,2,3,[4,5,6]]
.
Two quick and easy ways are two use Array.concat or the spread operator.
const arr1 = [1,2,3];
const arr2 = [4,5,6];
// With concat
console.log(arr1.concat(arr2));
// With spread
console.log([...arr1, ...arr2])