语法:Array.concat(val1[,val2[,val3...[,valN]]])
参数:
valN:可以填数组或值,把他们合并到一个新数组里,原数组不变,如果不写,则默认对Array进行一个浅复制
返回值:新的数组实例
话不多说直接上代码:
let arr1 = [1,2,3];
let arr2 = [4,5,6];
let arr3 = [7,8,9];
let num = 10;
console.log(arr1.concat(arr2,arr3,num));
// Array: [1,2,3,4,5,6,7,8,9,10]
// 不写参数默认对调用他的那个数组进行一个浅拷贝,原数组不变
console.log(arr1.concat());
// Array: [1,2,3]
// 原数组不会发生改变
console.log(arr1)
// Array: [1,2,3]
console.log(arr2)
// Array: [4,5,6]
console.log(arr3)
// Array: [7,8,9]
注意:concat不会把数组里的嵌套元素也给合并到一起:比如
let arr1 = [1,2,3];
let arr2 = [4,[5,6]];
let num = 7;
let arr3 = [8,9];
console.log(arr1.concat(arr3));
// Array: arr1+arr3:[1,2,3,8,9]
console.log(arr1.concat(arr2));
// Array: arr1+arr2:[1,2,3,4,[5,6]]
console.log(arr1.concat(num));
// Array: arr1+num:[1,2,3,7]
console.log(arr1.concat(arr2,arr3));
// Array: arr1+arr2+arr3:[1,2,3,4,[5,6],8,9]
console.log(arr1.concat(arr2,num,arr3));
// Array: arr1+arr2+num+arr3:[1,2,3,4,[5,6],7,8,9]