Sometimes I like to use this kind of pattern for flattening nested data (arrays, tree structures and the like)
const stuff = [1, [2, [3, 4]]];
let pile = [stuff];
for (let item of pile) {
if (item instanceof Array) pile.push(...item);
else console.log(item);
}
It occurred to me though that I'm just assuming that
for (let item of pile) { ...
is syntax candy for
for (let i = 0; i < pile.length; i++) { const item = pile[i]; ...
while it could also be syntax candy for
for (let i = 0, length = pile.length; i < length; i++) { const item = pile[i]; ...
Right now it works the way I expect but is it guaranteed to work or is it implementation specific?