jquery - Javascript: Iterating over array with non-consecutive keys -
jquery - Javascript: Iterating over array with non-consecutive keys -
i need iterate on array keys non-consecutive:
var messages = new array(); messages[0] = "this first message"; messages[3] = "this message";
obviously using index of loop not work depends on keys beingness sequential:
for (var i=0 ; i<messages.length ; i++) { alert(messages[i]); // alert first message, never equal 3 }
what canonical way of dealing this, seeing the for-each syntax not intended iterating on values in array in javascript? thanks.
the idiomatic way utilize object, not array. sure check hasownproperty
create sure don't pick stray things may have been added prototype.
var messages = { }; messages[0] = "this first message"; messages[3] = "this message"; (var in messages) { if ({}.hasownproperty.call(messages, i)) alert(messages[i]); }
javascript jquery oop iterator
Comments
Post a Comment