There's a lot of hullaballoo some days about "you should use reduce more" or "you don't need a filter, use a map", or "For? Why not forEach?"
The ...
For further actions, you may consider blocking this person and/or reporting abuse
Liquid syntax error: 'raw' tag was never closed
While using
&&
to actually return the latter is indeed a cool trick, it's probably confusing for beginners. And in my opinion the ternary operator comes in more handy in this case.Here's a simple approach which uses the ternary operator and should also be pretty easy to understand for beginners:
That works, because
is basically the same as
(Think of
?
and:
as 'then' and 'else')And since we can leave away the curly braces in this case (since it's just one expression), we can simply use this arrow function:
So this was a brief explanation (hopefully someone understands what I'm saying) of the ternary operator, see MDN to learn more
I'll just briefly explain what that
&&
does, for everyone not understanding what you mean:&&
returns the value of the right-hand expression when both are truthy.Same with
||
: It returns the left-hand expression if it's truthy, or the right-hand one of the first one was falsey.See the descriptions of the logical operators (and the examples) on MDN for more information.
So
&&
can be used like 'when the left expression is truthy, return the right-hand (or run a function)'.E.g.
And
||
can be used to provide a default value (or also run a function or something).Hope this was not too confusing.
If i would ever seen such code i would instantly decline any pull request this may be contained in.
Do you seriously mean that it make it simpler in any way? I have been programing node.js apps for a 3 years now and seen some horrible things done in sake of simplicity and performance and this may be one of those. What if acumulator have started as
-1
for some reason? You would have never change it in the end.I understand how the ternary and the logical operators work, and still I agree with you that it doesn't make it "simpler". If and else takes some more lines of code, but it's more easy to debug, refactor, document compared to single line expressions.
You are right, when accumulator stars in -1, it'll fail. When I said much simpler, I meant simpler in terms of writing code. :D
Protip: Don't try to be clever with your code:
simplethread.com/dont-be-clever/
Has no one mentioned filter!?
I think a better example of
reduce
would be something that can't be done withfilter
.return (accumulator += (item === "Bob" ?1:0));
this way(using "?:" or "&&||" after equal sign) works better in lots of situation
That
=
in+=
is redundant since the reducer only needs to return the new value, not change the old one. I mean, it'll still work, because the correct value is returned, but there's no need in changing the passed-in accumulator.return accumulator + Number(item === "Bob");
Glad it's been helpful for so many folks already!
Yes, some of these functions could be reduced (pun intended) to an even smaller and possibly more performant size using
&&
, ternary operators, or++accumulator
instead ofaccumulator + 1
. If you're interested in how to take your code to the next level with some of those refactors, definitely read the other comments here!I intentionally stayed away from some of those 'intermediate level' code 'tricks' for the sake of making these examples as readable as possible. ;)
Nice article! Everyone seems to forget my two favorite functions though,
some
andevery
If you've ever written something like
If you ever find yourself checking the
length
property afteritems.filter()
, you can likely usesome
orevery
to do the same thing without the array generation penalty.Really nice!!! I'm a Python programmer and didn't know JS has functions like that!! Congrats bro
Best explanation of
reduce
I've found to date. awesome!Do you know if
forEach
is better in term of perf thanfor
?Nice post !
According to this
for
is pretty much always faster thanforEach
, so if performance is more important than concise code (and it usually should be) afor
loop is likely a better choice.Correct, as the
forEach
call will incur a penalty invoking the lambda expression.forEach
could have equivalent performance if the JIT inlines the lamba method body, but it's safe to say always expectforEach
to be a bit slower.Ya, this one is hot:
originalArray.forEach( item => doSomething(item); );
Zamob.co.za is one of the largest mobile web portal in South Africa, where mobile users can download lots of android games, videos, mp3 music and java games all for free. You can download millions of free Games, Apps, Music....
Great article dude. Well written and easy to understand. You might want to reference at least once the arrow functions real name is a Lambada function. Can't wait to read more
Thanks Peter. I believe you're referencing lambda expressions, which are expressions which return a function. While it's true that Javascript arrow functions are comprised of a syntax which enable shorter definitions of expressions which return functions, very very few people in the Javascript community refer to these as lambda expressions. Doing so promotes confusion for newer developers more than helping them, as searching for 'Javascript lambda functions' just redirects them back to 'arrow functions'. The official standard also calls them arrow functions, so that's what I went with here.
Thanks for the reply. And the spelling correction 😣 I should have used a real keyboard and not my thumbs. That's good to know that they're officially called arrow functions, I will make note of that for myself. Thanks again,
Pete
I always try to understand the contents of Multivitamins and Supplements been sold in the market before making best choice of what to go for.
If only I would have read this article back then!
I struggled my way to learn about these type of higher order functions with .NET's LINQ and Kotlin's standard library.
But this post is gold. I'll make sure to share it with friends.
This is great! As a beginner I am always confused between the Filter/Map/Reduce, it is super helpful to have this! Thank you
Thanks! I didn't know about reduce. I prefer to use the most simple and readable for the occasion and the most necessary
Why forEach, what about for...of and for...in ?
I included forEach here because this article was specifically about the iterator functions built into the Array object. for...of and for...in are super great tools, too, but were out of scope for this article. Will definitely include in the larger piece I'm working on writing. ;)
Well arrow functions still save us some code in case
reduce
.Wow.. i learnt something new today. Always some innovative methods on this space
This is some of the clearest JavaScript writing I've seen.
FouMovies Is one of the greatest movies and tv series website.
Nice one... Problem solved.
makrospecials.net/zamob/
Good, well educative to read, Thank You
mstwotoes.com/mp3paw-download-free...
Awesome! Thanks.
Great tutorial!! Got god understand about array stuff!
This breakdown of array functions, was exactly what I was looking for. Thanks for taking the time to explain them!
Thanks for your thoughts Rafael. This article is aimed at beginners, so I chose the easiest-to-read, more verbose way of expressing things.
WapTrick is very great for games, app, videos and movies
Wapdam is the new site which offer good movies and web series
Nice to see all the code and the explanations. Really clarifying on this end