方法一: unshift
+ splice
var arr1 = ['a', 'b', 'c'];
var arr2 = ['1', '2', '3'];
var index = 2;
arr2.unshift(index, 0);
Array.prototype.splice.apply(arr1, arr2);
console.log(arr1); // ["a", "b", "1", "2", "3", "c"]
方法二:splice
+ concat
var arr1 = ['a', 'b', 'c'];
var arr2 = ['1', '2', '3'];
var index = 2;
arr1.splice.apply(arr1, [index, 0].concat(arr2));
console.log(arr1); // ["a", "b", "1", "2", "3", "c"]
方法三: splice
+ ES6 rest
var arr1 = ['a', 'b', 'c'];
var arr2 = ['1', '2', '3'];
arr1.splice(2, 0, ...arr2)
console.log(arr1) // ["a", "b", "1", "2", "3", "c"]
网友评论