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.
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…
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…
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…
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…
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){
…
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…
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…
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…
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…
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…
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…
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…
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…
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…