题目描述

在数组 arr 的 index 处添加元素 item。不要直接修改数组 arr,结果返回新的数组

示例1

输入

[1, 2, 3, 4], 'z', 2

输出

[1, 2, 'z', 3, 4]
function insert(arr, item, index) {
    var arr1 = arr.slice(0, index)
    var arr2 = arr.slice(index)
    console.log(arr1)           // [1, 2]
    console.log(arr2)           // [3, 4]
    return arr1.concat(item, arr2)   // [1, 2, "z", 3, 4]
}
var arr = [1, 2, 3, 4]
insert(arr, 'z', 2)

// --------------------------------------------------------
function insert(arr, item, index) {
    var a = [].concat(arr)
    a.splice(index, 0, item)
    return a      // [1, 2, "z", 3, 4]
}

// -------------------------------------------------------
//利用slice+concat
function insert(arr, item, index) {
    return arr.slice(0,index).concat(item,arr.slice(index));
}
//利用concat +splice
function insert(arr, item, index) {
    var newArr=arr.concat();
    newArr.splice(index,0,item);
    return newArr;
}
//利用slice+splice
function insert(arr, item, index) {
    var newArr=arr.slice(0);
    newArr.splice(index,0,item);
    return newArr;
}
//利用push.apply+splice
function insert(arr, item, index) {
    var newArr=[];
    [].push.apply(newArr, arr);
    newArr.splice(index,0,item);
    return newArr;
}
//普通的迭代拷贝
function insert(arr, item, index) {
    var newArr=[];
    for(var i=0;i<arr.length;i++){
        newArr.push(arr[i]);
    }
    newArr.splice(index,0,item);
    return newArr;
}

results matching ""

    No results matching ""