发布网友 发布时间:2022-04-26 16:00
共1个回答
热心网友 时间:2022-04-22 20:23
function insert(arrfirst,arrlast,index){ //将数组arrlast插入数组arrfirst中,index是想要插入的位置
if (index < 0){
index = 0;
}else if(index > arrfirst.length){
index = arrfirst.length;
}
var arr = [];
for (var i = 0; i < index; i++){
arr.push(arrfirst[i]);
}
for (var i = 0; i < arrlast.length; i++){
arr.push(arrlast[i]);
}
for (var i = index; i < arrfirst.length; i++){
arr.push(arrfirst[i]);
}
return arr;
}
var arr1 = ["1","2","3"];
var arr2 = ["a","b","c"];
console.log(insert(arr1,arr2,0));
alert(insert(arr1,arr2,1));
还有一种方法
function insert(arrfirst,arrlast,index){希望能帮到你