You can use the splice
method to replace part of an array with items from another array, but you have to call it in a special way as it expects the items as parameters, not the array.
The splice
method expects parameters like (0, anotherArr.Length, 1, 2, 3)
, so you need to create an array with the parameters and use the apply
method to call the splice
method with the parameters:
Array.prototype.splice.apply(arr, [0, anotherArr.length].concat(anotherArr));
Example:
var arr = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
var anotherArr = [ 1, 2, 3 ];
Array.prototype.splice.apply(arr, [0, anotherArr.length].concat(anotherArr));
console.log(arr);
Output:
[ 1, 2, 3, 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Demo: http://jsfiddle.net/Guffa/bB7Ey/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…