Questions tagged [for-of-loop]

Use for-of-loop for questions related to the for...of statement used for iterating over an iterable collection.

References

107 questions
286
votes
12 answers

Access to ES6 array element index inside for-of loop

We can access array elements using a for-of loop: for (const j of [1, 2, 3, 4, 5]) { console.log(j); } How can I modify this code to access the current index too? I want to achieve this using for-of syntax, neither forEach nor for-in.
Abdennour TOUMI
  • 64,884
  • 28
  • 201
  • 207
38
votes
1 answer

How can I turn off ESLint's no-restricted-syntax rule just for ForOfStatement?

I am using ESLint for my ES6 program, with the AirBNB rule-set. For good and adequate reasons, I am using the for...of construction in my code, but ESLint objects to it, issuing a no-restricted-syntax error. The documentation at…
Mike Taylor
  • 393
  • 1
  • 3
  • 5
20
votes
4 answers

for...of loop. Should I use const or let?

When using a for of loop, both of these are allowed and work: const numbers = [1,2,3]; // works for(let number of numbers) { console.log(number); } // also works for(const number of numbers) { console.log(number); } I always use const since…
17
votes
3 answers

How can a Javascript object become iterable with for...of statement?

I would like to set the options[Symbol.iterator] property in order to iterate on the simple objects I create with the for...of statement : options = { male: 'John', female: 'Gina', rel: 'Love' }; for(let p of options){ …
cicciosgamino
  • 677
  • 1
  • 7
  • 25
15
votes
5 answers

How to iterate over a Set or Map in reverse order in javascript?

I'm looking for a a way to iterate over a Set or Map in reverse order. Consider this simple example in regular order: var mySet = new Set([1,2,3,4,5]); for(let myNum of mySet) { console.log(myNum); // output: 1, 2, 3, 4, 5 in sepearte lines } The…
Holger Stitz
  • 1,601
  • 3
  • 14
  • 19
11
votes
2 answers

The meaning of "'x' is not a function or its return value is not iterable" error

I accidentally witnessed that this causes an error in V8 (Chrome, Node.js, etc): for (let val of Symbol()) { /*...*/ } TypeError: Symbol is not a function or its return value is not iterable It appears that any other non-iterable value (including…
Estus Flask
  • 150,909
  • 47
  • 291
  • 441
7
votes
1 answer

Can I use continue and break in Javascript for...in and for...of loops?

Can I use the break and continue statements inside the for...in and for...of type of loops? Or are they only accessible inside regular for loops. Example: myObject = { propA: 'foo', propB: 'bar' }; for (let propName in myObject) { if…
cbdeveloper
  • 14,014
  • 11
  • 57
  • 145
6
votes
3 answers

Remove array item using for...of loop

I'm trying to edit an array and remove elements that do not meet a certain condition. If I use a reverse for loop combined with .splice(index,n), the code works just fine. I'm stuck at implementing the same using the ES6 for...of loop let…
danieln
  • 353
  • 2
  • 16
5
votes
2 answers

for..in or for..of Object keys

So my IDE doesn't like when I use a for..in loop to iterate over an object keys. I get a warning: Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check So I get what it's saying, so in that case…
Spedwards
  • 4,080
  • 13
  • 36
  • 84
4
votes
2 answers

Why does this incorrect JavaScript program produce the correct answer?

I was given the following JavaScript program in an interview. const average = xs => { let sum = 0; for (let num in xs) sum += num; return sum / xs.length; }; const result = average([2, 4, 6]); console.log(result); // 4 The…
Aadit M Shah
  • 67,342
  • 26
  • 146
  • 271
4
votes
4 answers

Type 'HTMLCollectionOf' must have a '[Symbol.iterator]()' method that returns an iterator

I am building an Array with const myCanvas = documen.getElementsByTagName('canvas') that it's actually working. It returns me something like this: images: [ 0: canvas, 1: canvas, 2: canvas ] This is for a Typescript project, I want to iterate this…
Jgascona
  • 811
  • 1
  • 7
  • 20
4
votes
2 answers

How does `for..of` loop resolve the iterator from an object?

For an object to implement iterable interface it must implement [Symbol.iterator] key that points to a function that returns the iterator. I'm wondering if the for..of loop internally calls this method on an object to get that iterator? The reason…
Max Koretskyi
  • 85,840
  • 48
  • 270
  • 414
4
votes
3 answers

Javascript simple for loop versus for...of performances

I have seen that since ECMA 6 we can use for...of instead of the traditionnal for loop: for( let i = 0 ; i < arr.length ; i++ ) { var elm = arr[i]; // do stuff } VS for( let elm of arr ) { // do stuff } Has you see the second one is…
GLAND_PROPRE
  • 3,318
  • 2
  • 21
  • 47
4
votes
2 answers

Creating Promises in for...of loop instead forEach?

I want to execute an array of Promises in parallel and then wait until all Promises are executed. This works: var promises = []; objects.forEach(function(object) { let promise = new Parse.Promise.as() .then( function() { …
Manuel
  • 11,849
  • 3
  • 39
  • 93
4
votes
2 answers

How does Break work in for-of loop when stopping a Generator?

So there are some ways to stopping a Generator in for of loop, but how does break send a signal to the Generator(in comparison with return in for-of)? please consider the code. As an example, the preceding code just increases a value from 1 to 10…
Mehdi Raash
  • 6,893
  • 1
  • 26
  • 40
1
2 3 4 5 6 7 8