JavaScript array splicing method

In JavaScript, you can use concat()methods to combine two or more arrays into one array. This method returns a new array containing the merged array elements. For example:

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1.concat(arr2);
console.log(arr3); // [1, 2, 3, 4, 5, 6]

Alternatively, ...two or more arrays can be joined using the spread operator. For example:

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [...arr1, ...arr2];
console.log(arr3); // [1, 2, 3, 4, 5, 6]

It should be noted that both methods return a new array, and the original array will not be modified. If you want to modify the original array, you can use push()the or splice()method to add new elements.

Guess you like

Origin blog.csdn.net/m0_72446057/article/details/129253896