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

当前回答

尝试在循环时将数组中继到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个元素”在ES5上下文中。

JavaScript数组有不同的方法从开始或结束添加/删除元素。这些都是:

arr.push(ele) - To add element(s) at the end of the array 
arr.unshift(ele) - To add element(s) at the beginning of the array
arr.pop() - To remove last element from the array 
arr.shift() - To remove first element from the array 

基本上上述方法都不能直接用于从数组中删除第n个元素。

一个值得注意的事实是,这与java迭代器的相反 使用它可以删除集合的第n个元素 而迭代。

这基本上只留给我们一个数组方法array。splice来执行删除第n个元素(你也可以用这些方法做其他事情,但在这个问题的上下文中,我主要关注的是删除元素):

Array.splice(index,1) - removes the element at the index 

以下是从原始答案复制的代码(带有注释):

Var arr = [" 1 ", " 2 ", " 3 ", " 4 "]; Var I = arrr .length;//初始化计数器为数组长度 while (i——)//递减计数器,否则将遇到IndexOutBounds异常 { If (arr[i] === "four" || arr[i] === "two") { //splice修改原始数组 加勒比海盗。拼接(我,1);//永远不会遇到IndexOutBounds异常 console.log(“元素移除。加勒比海盗:”); }其他{ console.log("元素未删除。加勒比海盗:”); } console.log (arr); }

另一个值得注意的方法是Array.slice。然而,此方法的返回类型是删除的元素。这也不会修改原始数组。修改后的代码片段如下:

Var arr = [" 1 ", " 2 ", " 3 ", " 4 "]; Var I = arrr .length;//初始化计数器为数组长度 While (i——)//自减计数器 { If (arr[i] === "four" || arr[i] === "two") { console.log(“元素移除。加勒比海盗:”); console.log(加勒比海盗。Slice (i, i + 1)); console.log("原始数组:"); console.log (arr); } }

话虽如此,我们仍然可以使用Array。切片以删除第n个元素,如下所示。然而,它有更多的代码(因此效率低)

Var arr = [" 1 ", " 2 ", " 3 ", " 4 "]; Var I = arrr .length;//初始化计数器为数组长度 While (i——)//自减计数器 { If (arr[i] === "four" || arr[i] === "two") { console.log("删除第i个元素后的数组:"); Arr = Arr。片(0,1).concat(加勒比海盗。Slice (i + 1)); console.log (arr); } }

数组。切片法的实现极为重要 函数式编程中的不可变性à la redux

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

你可以浏览一下,然后使用shift()

虽然你的问题是关于从被迭代的数组中删除元素,而不是关于有效地删除元素(除了一些其他处理),但我认为如果遇到类似情况,应该重新考虑它。

这种方法的算法复杂度是O(n^2)作为拼接函数和for循环都遍历数组(在最坏的情况下,拼接函数移位数组的所有元素)。相反,您可以将所需的元素推入到新数组中,然后将该数组赋值给所需的变量(该变量刚刚被迭代)。

var newArray = [];
for (var i = 0, len = Auction.auctions.length; i < len; i++) {
    auction = Auction.auctions[i];
    auction.seconds--;
    if (!auction.seconds < 0) { 
        newArray.push(auction);
    }
}
Auction.auctions = newArray;

自ES2015以来,我们可以使用Array.prototype.filter将所有内容都放在一行中:

Auction.auctions = Auction.auctions.filter(auction => --auction.seconds >= 0);

举两个例子:

一个例子

// 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
        }
    }
}