其他分享
首页 > 其他分享> > JS 数组中指定位置插入另一个数组

JS 数组中指定位置插入另一个数组

作者:互联网

You can use splice combined with some apply trickery:

a1 = [1,2,3,4,5];
a2 = [21,22];

a1.splice.apply(a1, [2, 0].concat(a2));

console.log(a1); // [1, 2, 21, 22, 3, 4, 5];

In ES2015+, you could use the spread operator instead to make this a bit nicer

a1.splice(2, 0, ...a2);

转载: https://stackoverflow.com/questions/7032550/javascript-insert-an-array-inside-another-array

标签:use,数组,JS,a1,插入,a2,splice,apply,array
来源: https://blog.csdn.net/javaStudyeye/article/details/122548788