Categories
JavaScript Answers

How to use the JavaScript array splice method with the third parameter as an array?

Spread the love

Sometimes, we want to use the JavaScript array splice method with the third parameter as an array.

In this article, we’ll look at how to use the JavaScript array splice method with the third parameter as an array.

How to use the JavaScript array splice method with the third parameter as an array?

To use the JavaScript array splice method with the third parameter as an array, we can use the spread operator with splice.

For instance, we write:

const a1 = ['a', 'e', 'f'];
const a2 = ['b', 'c', 'd'];

a1.splice(1, 0, ...a2);
console.log(a1)

to call a1.splice with 1, 0 and the entries of the a2 array as arguments.

This inserts 'b', 'c' and 'd' between 'a' and 'e' in `a“.

Therefore, a1 is ['a', 'b', 'c', 'd', 'e', 'f'].

Conclusion

To use the JavaScript array splice method with the third parameter as an array, we can use the spread operator with splice.

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 *