我有下面的for循环,当我使用splice()要删除一个项,我将得到“秒”是未定义的。我可以检查它是否是未定义的,但我觉得可能有更优雅的方法来做到这一点。我们的愿望是简单地删除一个项目并继续进行下去。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);
} }循环遍历数组并删除项,而不中断for循环。
3 回答
青春有我
TA贡献1784条经验 获得超8个赞
.splice()
.length
i
.splice()
var i = Auction.auctions.lengthwhile (i--) { ... if (...) { Auction.auctions.splice(i, 1); } }
慕后森
TA贡献1802条经验 获得超5个赞
for (var i = Auction.auctions.length - 1; i >= 0; i--) { Auction.auctions[i].seconds--; if (Auction.auctions[i].seconds < 0) { Auction.auctions.splice(i, 1); }}
慕仙森
TA贡献1827条经验 获得超7个赞
for (i = 0; i < Auction.auctions.length; i++) { auction = Auction.auctions[i]; Auction.auctions[i]['seconds'] --; if (auction.seconds < 0) { Auction.auctions.splice(i, 1); i--; //decrement }}
添加回答
举报
0/150
提交
取消