I have an array created with this code:
var widthRange = new Array();
widthRange[46] = { min:0, max:52 };
widthRange[66] = { min:52, max:70 };
widthRange[90] = { min:70, max:94 };
I want to get each of the values 46, 66, 90 in a loop. I tried for (var key in widthRange)
but this gives me a whole bunch of extra properties (I assume they are functions on the object). I can't use a regular for loop since the values are not sequential.
Array.prototype.forEach
. CallingObject.keys
on an array is illperformant because browsers don't optimize for it.for(var key in array)
is bad because it traverses the prototype and stringifies each number key it encounters (converting doubles to base10 is very slow).forEach
, however, was designed exactly for the purpose of iterating sparse arrays and will give your code excellent performance compared to other solutions