1

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]].

HackerMan
  • 200
  • 1
  • 13
  • 1
    `.concat()` instead of `.push()` should work (`.concat` will return a new array rather than changing it place though) – Nick Parsons Dec 24 '19 at 00:37

1 Answers1

0

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])
mwilson
  • 12,295
  • 7
  • 55
  • 95