I'm aware that arrays in JavaScript differ from your traditional arrays in the sense that they are just objects under the hood. Because of this, JavaScript allows for sparse arrays to behave in a similar manner to dense arrays when it comes to memory management. When working with a sparse array, is there a way to reach the next element in that array efficiently?
For example, given this array:
var foo = [];
foo[0] = '0';
foo[1] = '1';
foo[2] = '2';
foo[100] = '100';
console.log(foo.length); // => 101
I'm know that there's a way to get all of the elements by using for ... in like so:
for (var n in foo){
console.log(n);
}
// Output:
// 0
// 1
// 2
// 100
However, is there an explicit way to simply get from one of these elements to the next?
For example, does there exist some way to achieve similar behavior to this?
var curElement = foo[2]; // => 2 (the contents of foo[2])
var nextElement = curElement.next(); // => 100 (the contents of foo[100])
// ^^^^^^
// Not an actual function, but does there exist some way to potentially
// mimic this type of behavior efficiently?
undefinedvalues. Surely there must be some more efficient way?