Linked Questions
11 questions linked to/from Why should forEach be preferred over regular iterators?
177 votes
2 answers
98k views
Should one use for-of or forEach when iterating through an array? [duplicate]
Also, is this a style question or a functional question? Is it a matter of preference or is one better? I'm trying to understand the purpose of for-of. Usually I use, let iterable = [10, 20, ...
3343 votes
35 answers
2.5m views
Using async/await with a forEach loop
Are there any issues with using async/await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function ...
2244 votes
32 answers
1.9m views
Short circuit Array.forEach like calling break
[1,2,3].forEach(function(el) { if(el === 1) break; }); How can I do this using the new forEach method in JavaScript? I've tried return;, return false; and break. break crashes and return does ...
43 votes
8 answers
22k views
Convert an array of objects to array of the objects' values
I am trying to convert this array let orders = [ { amount: '100', user: 'admin', date: 'March 6, 2019' }, { amount: '120', user: 'admin', date: 'March 6, 2019' }, { amount: '80', user: '...
26 votes
5 answers
10k views
Javascript yield from the function nested inside generator
This code generates an error: function *giveNumbers() { [1, 2, 3].forEach(function(item) { yield item; }) } This is probably because yield is inside a function that is not a generator....
4 votes
6 answers
28k views
Array Map function not able to push Object in simple Example
I got data where in an array I got set of objects (with country and states keys along with values). Now, I need to format data in such a manner, that I got a new array (with countryName as key, and ...
5 votes
2 answers
3k views
Higher perf - Map.prototype.forEach or for..of?
Say I have: const m = new Map(); is there a performance difference between: for(let v of m){ } vs. m.forEach(v => {}); I assume Map.protoype.forEach uses an iterator just like for..of? I read ...
-3 votes
3 answers
174 views
a bit clarafication on using spread and string manipulation, foreach loop
so I've wrote this function, i want to uppercase the vowels and lowercase every other letter, problem the end result ends with the same string, I'm new to spread and for-each, after i spread a string ...
1 vote
1 answer
74 views
What advantage(s) do some built-in methods provide by taking a synchronous callback as their argument?
I'm trying to understand callbacks thoroughly and this is the fundamental missing piece. I've scoured the internet for this answer but most just talk about async callbacks. Q 1: Why do some built-in ...
1 vote
1 answer
153 views
How can I efficiently copy the entries from one Map to another Map where the target Map has multiple reference variables?
I have found a way to copy the entries from one Map to another Map where the target Map has multiple reference variables but I suspect it is not optimal. Is there a shorter/more efficient way? const ...
0 votes
0 answers
42 views
How can I write this function with a forEach loop? [duplicate]
So I'm working on a FreeCodeCamp javascript problem. I was able to write a working function using a for loop, but when I try to write the same function with a forEach loop, it doesn't work. How can I ...