JavaScript中像数组的头部插入一个数组
在JavaScript中,可以使用Array.prototype.unshift()方法将一个数组插入到另一个数组的头部。下面是一个示例:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
Array.prototype.unshift.apply(array1, array2);
console.log(array1); // [4, 5, 6, 1, 2, 3]
在上面的示例中,我们使用Array.prototype.unshift.apply()方法将array2插入到array1的头部。这个方法会将array2中的每个元素依次插入到array1的头部。最后,我们输出array1的值,得到[4, 5, 6, 1, 2, 3]。
原文地址: https://www.cveoy.top/t/topic/iEby 著作权归作者所有。请勿转载和采集!