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

157 questions
437
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
  • 87,526
  • 38
  • 249
  • 254
66
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…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
60
votes
2 answers

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
  • 733
  • 1
  • 5
  • 5
36
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
  • 27,898
  • 37
  • 155
  • 336
27
votes
4 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
  • 2,011
  • 3
  • 17
  • 19
20
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
  • 871
  • 3
  • 10
  • 29
19
votes
5 answers

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

I am building an Array with const myCanvas = document.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…
Jgascona
  • 1,201
  • 2
  • 13
  • 25
18
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
  • 206,104
  • 70
  • 425
  • 565
11
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
  • 473
  • 1
  • 5
  • 20
10
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,167
  • 16
  • 49
  • 106
9
votes
2 answers

What is the difference between iterable and enumerable in JS? I am going through For/of and For/In loop and these terms are coming up frequently

I am coming across terms Iterable and Enumerable while studying For/in and For/of loops. Objects are supposed be enumerable and we have to use For/in loop to loop over the properties of the object and For/of to loop over the arrays and strings. I…
Mahwash
  • 91
  • 1
  • 4
7
votes
2 answers

For-Of Loop vs. For Loop

Are these two the same or interchangeable? What are some use cases that someone would choose one over the other? for(let i of array){some code} for(let i = 0; i < array.length; i++){some code} EXAMPLE Complete the solution so that the function…
and1
  • 307
  • 2
  • 10
6
votes
1 answer

for...of an iterator without closing it

Is it possible to loop over part of an iterator with for...of without closing the iterator when I break the loop? Example: function* numbers(i=0){ while(true) yield i++; } let nums=numbers(); // this loop prints numbers from 0 to 3 for(const n…
Blue Nebula
  • 932
  • 4
  • 9
5
votes
1 answer

In JavaScript, Iterating over generator using for-of loop ignore return statement why?

Take a close look at code function* NinjaGenerator() { yield "yoshi"; return "Hattori"; } for (let ninja of NinjaGenerator()) { console.log(ninja); } // console log : yoshi Why "Hattori" is not logged ? Where as when we iterate over…
Faizanur Rahman
  • 474
  • 3
  • 12
5
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
  • 8,721
  • 2
  • 29
  • 42
1
2 3
10 11