javascript - Looping through array and removing items, without breaking for loop -


i have following loop, , when use splice() remove item, 'seconds' undefined. check if it's undefined, feel there's more elegant way this. desire delete item , keep on going.

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

the array being re-indexed when .splice(), means you'll skip on index when 1 removed, , cached .length obsolete.

to fix it, you'd either need decrement i after .splice(), or iterate in reverse...

var = auction.auctions.length while (i--) {     .... } 

this way re-indexing doesn't affect next item in iteration, since indexing affects items current point end of array, , next item in iteration lower current point.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -