Javascript iterating through sparse array
For..in isn't the worst thing when you're working on an object hash. It's to be avoided for use with arrays ([]
), but it should be ok here:
var val;
for (index in testArray) {
if (index == 10) {
break;
} else {
val = testArray[index];
}
}
What's wrong with the for...in
syntax? You have an object so the for...in
syntax is completely valid to use:
var testArray = { 0: "value1", 5: "value2", 10: "value3", 15: "value4" };
for (var key in testArray) {
var value = testArray[key];
if (...) {
break;
}
}