"The for...of statement creates a loop iterating over iterable objects (including Array, Map, Set, String, TypedArray, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property."
In Python you can iterate over iterators or generators. When the end of iteration is reached, an exception is raised that triggers the loop to stop. To me, for..of seems closer to python for..in.
for .. in iterates over key, i.e. it's comparable to something like:
for (let i = 0, o = Object.keys(X); i < o.length; i++, x=o[i]) { .. }
whereas for .. of iterates over iterables i.e. it's roughly comparable to something like:
for (let x, o; o && o.done === false; o = X.next(), x = o && o.value) { .. }
It's slightly more complicated than that because it implicitly converts some objects, such as Arrays or Maps, to iterables.
JavaScript since ES6 also has generators and iterators that work somewhat similar. There are no exceptions, tho, because iterator interface (that generators also need to adher to in general) returns an object on calls to .next() that is of form {done, value}. When done is false you've reached the end of the iterable.
But note that in python for key in {1: 10, 2: 20}: iterates over dictionary's keys.
You can use for value in d.keys():, for (key, value) in d.items(): and (redundant) for k in d.keys():.
These methods exist in both Python 2 and 3, though with some differences (see python.org/dev/peps/pep-3106/).
You can drop the parenthesis i.e. for k, v in thing.items(). You only need parenthesis for comprehension i.e. {k: v for (k,v) in thing.items()} or [(k,v) for (k,v) in thing.items()]
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
"The for...of statement creates a loop iterating over iterable objects (including Array, Map, Set, String, TypedArray, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property."
In Python you can iterate over iterators or generators. When the end of iteration is reached, an exception is raised that triggers the loop to stop. To me, for..of seems closer to python for..in.
You're right.
for .. in
iterates over key, i.e. it's comparable to something like:whereas
for .. of
iterates over iterables i.e. it's roughly comparable to something like:It's slightly more complicated than that because it implicitly converts some objects, such as Arrays or Maps, to iterables.
JavaScript since ES6 also has generators and iterators that work somewhat similar. There are no exceptions, tho, because iterator interface (that generators also need to adher to in general) returns an object on calls to
.next()
that is of form{done, value}
. Whendone
is false you've reached the end of the iterable.But note that in python
for key in {1: 10, 2: 20}:
iterates over dictionary's keys.You can use
for value in d.keys():
,for (key, value) in d.items():
and (redundant)for k in d.keys():
.These methods exist in both Python 2 and 3, though with some differences (see python.org/dev/peps/pep-3106/).
You can drop the parenthesis i.e.
for k, v in thing.items()
. You only need parenthesis for comprehension i.e.{k: v for (k,v) in thing.items()}
or[(k,v) for (k,v) in thing.items()]