Pregunta

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?

¿Fue útil?

Solución

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top