我有以下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
        }
    }
}

其他回答

Auction.auctions = Auction.auctions.filter(function(el) {
  return --el["seconds"] > 0;
});

为什么在.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

尝试在循环时将数组中继到newArray:

var auctions = Auction.auctions;
var auctionIndex;
var auction;
var newAuctions = [];

for (
  auctionIndex = 0; 
  auctionIndex < Auction.auctions.length;
  auctionIndex++) {

  auction = auctions[auctionIndex];

  if (auction.seconds >= 0) { 
    newAuctions.push(
      auction);
  }    
}

Auction.auctions = newAuctions;

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

当我运行这个代码片段时,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;
          })

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