How forEach can be totally useless
javascript forEach is not a function (?)

Search for a command to run...
javascript forEach is not a function (?)

Hahaha I'm flattered to be such inspiration to you with this article. It's dumb of me to program all this years without even knowing the foreach's existance... But now with this I know a little more. Txs and keep the good teaching ^^
It's not dumb at all! As I said
"go by so many years without using it and by no means being powerless in his ability to make quality code."
Thanks for the inspiration and for stopping by!
I usually avoid forEach in JavaScript because it at times behaves weirdly.
I prefer map, filter and reduce methods, or the plain classic or the while sometimes. 😅
I usually avoid it in JS too!
Never underestimate the power of the classic syntax. It survived so many years and a lot of great software is built upon it.
You probably know better

The first rule of war and business is to know your enemy

Easy and responsive

Reusability with the least effort

It works no matter if it is real, FIFA, or eFootball

Good day, fellow reader.
Two weeks ago I was talking to a colleague, who has plenty of years of PHP expertise, and he was amazed by his personal discovery of the "for each" loop. I couldn't hide my surprise at how could that knowledge be elusive to him, but after my initial reaction, that could be portrayed like this:
Kramer from Seinfeld (TV Series)
I said: "well, it's useful depending on the case". He stated that many times he needed to apply the same behaviour to every page in a set of pages. And how that structure seemed more natural for him, so he was using it as much as he could.
That got me thinking into the real concept behind the for each loop, how can it be used, abused. I also wonder, how he could go by so many years without using it and by no means being powerless in his ability to make quality code.
So, what is actually a for each? It's nothing more than a for loop, that takes into consideration every item in a set of items, and applies (hopefully) some action to it.
Disclaimer: "for each" loop may vary from language to language.
Yeah, yeah, you got it. I just like the word traversing and wanted to include it in some text.
That implies two important things.
This doesn't mean you can't get the index of the current item inside a for each. It's just that it (usually) was not designed for it. Attempting that would mean extra work. From getting an index from a callback, up to finding the current element in the collection you are already iterating. It would be smarter just to use another kind of loop.
For each usually describes
"Apply this action to every item"
Instead of another kind of loops that are meant for
"Do this x times"
or
"Do this for items numbered a,b,x..."
It's not that easy for a generic article on this topic to make such a statement without proper benchmarks. Let me just say that if your dataset is too big, or the performance in your app is critical, you should consider this. Write in the comments if you disagree or can prove me wrong.
for each item in collection
// do this to item
ES6 introduced the for...of
for (let book of books) {
book.ISBN = getISBN();
}
Or the functional way
books.forEach(book => {
book.ISBN = getISBN();
}
foreach ($posts as $post) {
echo $post->title;
echo $post->body;
}
for beer in beers:
print(beer.name)
print(beer.IBU)
print(beer.ABV)
The interesting thing about foreach in python, is that it actually is the standard for loop. If you want the classic for (int i=0; i<array.length; i++)
for i in range(0, len(array)):
# Do whatever with i as index
See you around!
So tell me, do you find for each loop useful? In which language do you use it?