javascript - For loop in array reads 'remove'? -
this question has answer here:
- problems javascript “for in” loop 3 answers
i experienced strangest thing, code i'm using :
for (iter in data.list) { console.log(iter); }
as expect, log should give number of each row (0, 1, 2...), instead gives me :
0 1 2 remove
knowing array has 3 rows
did ever encountred ?
basically, problem iterating through array using for in
loop, not meant iteratung through arrays. intent iterate through properties of object, , apparently there property called remove
on array.
for more details on why for in
bad idea when comes arrays, see why using "for...in" array iteration bad idea?.
as solution, i'd suggest use indexed for
loop. type of loop not care properties, hence fine. comes down classical:
for (var = 0; < data.list; i++) { console.log(data.list[i]); }
by way: should not uppercase in javascript, unless it's constructor function. hence should data.list
.
ps: nice read when comes arrays , (mis-)using them, read fun javascript arrays, quite read.
Comments
Post a Comment