数组方法


js把一个数组内容插入到另一个数组的指定位置

let arr1 = ['a', 'b', 'c']; 
let arr2 = ['1', '2', '3']; 
// 把arr2 变成一个适合splice的数组(包含splice前2个参数的数组)
arr2.unshift(2, 0); // ------------------ 这里的数字控制插入的位置-------------------------
console.log(arr2); 
//[2, 0, "1", "2", "3"]
Array.prototype.splice.apply(arr1, arr2); 
console.log(arr1); 
//["a", "b", "1", "2", "3", "c"]

文章作者: Born to the sun
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Born to the sun !
评论