Frage

I noticed for...of loops were added to the ECMAScript-6 proposal, but have never heard of them until now. What's the typical use case for them?

War es hilfreich?

Lösung

The use case is when you want to do something with each element of an array. for...in in ECMAScript will sort of let you go over the array indices (and even that incorrectly).

The Python equivalent of ES6 for...of is for...in, like so:

myList = [ "a", "b", 5]
for el in myList:
  # el takes on the values "a", "b", 5 in that order
  pass
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top