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