JavaScript数组合并
方法一:
var a = [1,2,3]; var b=[4,5] a = a.concat(b); console.log(a); //此处输出为 [1, 2, 3 ,4 ,5]
方法二:
// ES5 的写法 var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; Array.prototype.push.apply(arr1, arr2); console.log(arr1)//[0,1,2,3,4,5]
方法三:
// ES6 的写法 var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; arr1.push(...arr2); console.log(arr1)//[0,1,2,3,4,5]
// ES6 的写法 var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; var arr3=[...arr1, ...arr2] console.log(arr3)//[0,1,2,3,4,5]