You wouldn't use forEach at all any more in ES6. You'd use a for of loop:
for (let div of document.querySelectorAll('div'))
div.style.color = 'green';
Apart from that, you can use Array.from to cast an iterable object to an array and then invoke .forEach on that; but in fact with the upcoming DOM spec this is unnecessary where querySelectorAll will return an Elements collection that does inherit from Array in the ES6 way - so you can call the .forEach method directly on it!
Alternative for [].forEach.call(...) in ECMA6Script
reactjs - Using find() inside forEach loop in javascript/es6 - Stack Overflow
JS Array.foreach vs for-loops, pros and cons, which do you use and why
If you want to do sequential async tasks, you can't use forEach, you need to use a for loop with await.
More on reddit.comHow to return a value from a forEach function (ES6)
Videos
You wouldn't use forEach at all any more in ES6. You'd use a for of loop:
for (let div of document.querySelectorAll('div'))
div.style.color = 'green';
Apart from that, you can use Array.from to cast an iterable object to an array and then invoke .forEach on that; but in fact with the upcoming DOM spec this is unnecessary where querySelectorAll will return an Elements collection that does inherit from Array in the ES6 way - so you can call the .forEach method directly on it!
You can use Array.from:
Array.from(elements).forEach(elem => ...)
It's probably a good idea to create a hash from id to item for array2 and use that to get the new names.
const idsToArray2 = array2.reduce( (acc, current) =>
{
acc[current.id]=current;
return acc
}, {})
array1.forEach((array1Item) => {
const foundItem = idsToArray2[array1Item.id];
if (foundItem) {
array1.newName = foundItem.name;
}
})
You could take two separate loops and collect in the first all id/name and map in the second new objects without mutating some data.
const
names = array2.reduce((m, { id, name: newName }) => m.set(id, { newName }), new Map),
result = array1.map(o => ({ ...o, ...names.get(o.id) }));
Whenever i loop through an array i personally prefer to use array.foreach to apply something to whatever element i wish to edit, but the industry standard seems to be using for loops. Is this becasue the industry isnt fully used to ES6 notation or is either method good in its respective ways? Anyways which method do you prefer to use, and why?
If you want to do sequential async tasks, you can't use forEach, you need to use a for loop with await.
Foreach, Map, Reduce, Filter, for..of, all these are good.
Now there are some cases where for..of or for loop will be substantially faster than a foreach for example, mainly because of the overhead caused by the callback in the foreach. But these are extreme cases, pretty rare, involving a high amount of elements being looped through.
In these cases you should always do some benchmark with different options to analyse the results and pick the best one anyway.
I prefer to use the aggregate functions over for loops because they're more expressive in my eyes. It's purely subjective though, and all the other options are fine too.