我有以下for循环,当我使用splice()删除一个项目时,我得到'seconds'是未定义的。我可以检查它是否未定义,但我觉得可能有一种更优雅的方式来做到这一点。他们的愿望是简单地删除一个项目,然后继续前进。

for (i = 0, len = Auction.auctions.length; i < len; i++) {
    auction = Auction.auctions[i];
    Auction.auctions[i]['seconds'] --;
    if (auction.seconds < 0) { 
        Auction.auctions.splice(i, 1);
    }           
}

当前回答

举两个例子:

一个例子

// Remove from Listing the Items Checked in Checkbox for Delete
let temp_products_images = store.state.c_products.products_images
if (temp_products_images != null) {
    for (var l = temp_products_images.length; l--;) {
        // 'mark' is the checkbox field
        if (temp_products_images[l].mark == true) {
            store.state.c_products.products_images.splice(l,1);         // THIS WORKS
            // this.$delete(store.state.c_products.products_images,l);  // THIS ALSO WORKS
        }
    }
}

两个例子

// Remove from Listing the Items Checked in Checkbox for Delete
let temp_products_images = store.state.c_products.products_images
if (temp_products_images != null) {
    let l = temp_products_images.length
    while (l--)
    {
        // 'mark' is the checkbox field
        if (temp_products_images[l].mark == true) {
            store.state.c_products.products_images.splice(l,1);         // THIS WORKS
            // this.$delete(store.state.c_products.products_images,l);  // THIS ALSO WORKS
        }
    }
}

其他回答

为什么在.splice上浪费CPU周期?该操作必须一次又一次地执行整个循环以删除数组中的一个元素。

为什么不只是在一个循环中使用传统的2个旗帜?

Const元素= [1,5,5,3,5,2,4]; Const remove = 5 I = 0 For(令j = 0;J < elements.length;j + +) { If(元素[j] !==删除){ 元素[i] =元素[j] 我+ + } } 元素。长度= I

这是这个简单线性时间问题的一个简单线性时间解。

当我运行这个代码片段时,n = 100万,每次调用filterInPlace()需要0.013到0.016秒。一个二次解(例如,公认的答案)将需要它的一百万倍左右。

// Remove from array every item such that !condition(item). function filterInPlace(array, condition) { var iOut = 0; for (var i = 0; i < array.length; i++) if (condition(array[i])) array[iOut++] = array[i]; array.length = iOut; } // Try it out. A quadratic solution would take a very long time. var n = 1*1000*1000; console.log("constructing array..."); var Auction = {auctions: []}; for (var i = 0; i < n; ++i) { Auction.auctions.push({seconds:1}); Auction.auctions.push({seconds:2}); Auction.auctions.push({seconds:0}); } console.log("array length should be "+(3*n)+": ", Auction.auctions.length) filterInPlace(Auction.auctions, function(auction) {return --auction.seconds >= 0; }) console.log("array length should be "+(2*n)+": ", Auction.auctions.length) filterInPlace(Auction.auctions, function(auction) {return --auction.seconds >= 0; }) console.log("array length should be "+n+": ", Auction.auctions.length) filterInPlace(Auction.auctions, function(auction) {return --auction.seconds >= 0; }) console.log("array length should be 0: ", Auction.auctions.length)

注意,这只是修改原始数组,而不是创建一个新数组;这样做是有好处的,例如,在数组是程序的单一内存瓶颈的情况下;在这种情况下,您不希望创建另一个相同大小的数组,即使是临时的。

删除参数

        oldJson=[{firstName:'s1',lastName:'v1'},
                 {firstName:'s2',lastName:'v2'},
                 {firstName:'s3',lastName:'v3'}]
        
        newJson = oldJson.map(({...ele}) => {
          delete ele.firstName;
          return ele;
          })

它删除和创建新的数组,因为我们在每个对象上使用展开运算符,所以原始数组对象也不会受到损害

普通的for循环对我来说更熟悉,我只需要在每次从数组中删除一个项时递减索引

//5个正确,5个错误 Var arr1 =[假,假,真,真,假,真,假,真,真,假]; //从数组中删除false For (var I = 0;I < arr1.length;我+ +){ If (arr1[i] === false){ arr1。拼接(我,1); I——;//如果item被移除,则递减索引 } } Console.log (arr1);//应该是5个true

当您执行.splice()时,数组正在重新索引,这意味着当一个索引被删除时,您将跳过一个索引,并且缓存的.length已过时。

要修复它,你要么需要在.splice()后面递减i,要么简单地反向迭代…

var i = Auction.auctions.length
while (i--) {
    ...
    if (...) { 
        Auction.auctions.splice(i, 1);
    } 
}

这样,重新索引就不会影响迭代中的下一项,因为索引只影响从当前点到数组末尾的项,并且迭代中的下一项低于当前点。