Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading