Categories
JavaScript Answers

How to insert an array inside another array with JavaScript?

Spread the love

Sometimes, we want to insert an array inside another array with JavaScript.

In this article, we’ll look at how to insert an array inside another array with JavaScript.

How to insert an array inside another array with JavaScript?

To insert an array inside another array with JavaScript, we can use the array splice method.

For instance, we write

a1.splice(2, 0, ...a2);

to call a1.splice with 2, 0 and the entries in array a2 as arguments.

We insert the elements of a2 from index 2 of a1 and we delete 0 entries from a1 before we insert them.

Conclusion

To insert an array inside another array with JavaScript, we can use the array splice method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *