3 ms·
If you need the index, you can use .keys() or .entries() on the iterable, e.g. for (const [index, value] of ["a", "b", "c", "d", "e"].entries()) { co
by Cthulhu_ 1y ago
If you need the index, you can use .keys() or .entries() on the iterable, e.g.
for (const [index, value] of ["a", "b", "c", "d", "e"].entries()) {
console.log(index, value);
}
Or forEach, or map. Basically, use a higher level language. The traditional for loop tells an interpreter "how" to do things, but unless you need the low level performance, it's better to tell it "what", that is, use more functional programming constructs. This is also the way to go for immutable variables, generally speaking.
- tgv 1y agoThere's no difference between for (x of a) stmt; and a.forEach(x => stmt), except for scope, and lack of flow control in forEach. There's no reason to prefer .forEach(). I don't see how it is "more functional."