How to add an new array to an existing array and return a new array in JavaScript?

I’m trying to add an new array to an existing array.

For example the existing array is

[[1,2,3], [4,5,6]]

And now I have a new array which is

[7,8,9]

How can I get

[[1,2,3],[4,5,6],[7,8,9]]

I tried to use the spread operator, but haven’t figured it out.

Thanks

>Solution :

You can push it:

const arr = [[1,2,3], [4,5,6]];
const arr2 = [7,8,9];
arr.push(arr2);

Or use the spread operator:

let arr = [[1,2,3], [4,5,6]];
const arr2 = [7,8,9];
arr = [...arr, arr2];

Leave a Reply