-3

I have an first array and the second array with elements I want to add to the first array

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

arr1.push(arr2);  // [ 1, 2, 3, [ 4, 5, 6]]  but i need [ 1, 2, 3, 4, 5, 6 ]

How can I do it?

3

2 Answers 2

4

Either, modify the array and use .concat:

arr1 = arr1.concat(arr2)

Or, push with the spread operator:

arr1.push(...arr2)
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it with spread operator:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.