I have a sparse array (indexes are not consecutive) like this:
var testArray = { 0: "value1", 5: "value2", 10: "value3", 15: "value4" };
I would simply like to iterate through each item, do some stuff, and be able to break under a certain condition.
I am fairly new to Javascript and I didn't find a proper way to do it. Here is what I tried:
Built-in "for..in". It seems that this is not the correct way to iterate through an array
forEach from ECMASCRIPT5. This one iterate correctly, but I cannot break from the loop.
_.each() from Underscore.js. Same result as #2.
$.each() from JQuery. With this one I can break by returning false, but it won't iterate correctly. For the above example, instead of iterating at 0, 5, 10, 15, it will iterate at 0,1,2,3,4,5,6... which is obviously not what I expect.
So my question is: Is there an easy way to iterate a sparse array with the possibility to break during the loop in Javascript or would it be better to use another data structure like an hashtable? If so, any recommandation?
Thanks!