JavaScript Is Everywhere, Even In Spaceships! Level Up Your Skills & Learn 11 Core JavaScript Functions To Remarkably Improve Code Quality.
Pa...
For further actions, you may consider blocking this person and/or reporting abuse
Worth pointing out that
startValue
inreduce
is optional. If omitted, it will take the first value from the array and the reducing will proceed from there. Care should be taken with this (passing an empty array will cause a TypeError), but it can result in more efficient code.Thanks for adding this! This is a really important addition.
Als, as you said: It should be taken with care because creating errors is really easy as reduce is complex :-D
Reduce is one of these things where you read it in an article like this one or in a bootcamp and think โyeah thatโs totally straightforwardโ but then you see what busted shit people are able to do with it in the wild and suddenly you feel like an ape in a cave ๐
Oh yeah. reduce is really weird :-D.
Normally, it looks so simple but in practice it destroy your sanity.
I didn't know about
every()
. Good to know.I used to use
filter()
and compare the resulting and the origin arrays' lengths, but that's nicer.Instead of
some()
I could also usefind()
. Is there any reason why I shouldn't use it instead?That's a good question.
As I know in terms of performance they are the same.
The main difference is that
find()
will return the first element which satisfies the provided function andsome()
will return true or false.So if you want to find AND return an element you should use
find()
. If you only want to know if the array contains the element you should usesome()
array-methods.github.io
This is really helpful!
thank you
I didn't know about the some() function before
No problem. Hopefully, you will use it now. It sometimes can help. Especially, if you only want to check if a given comparison is true for the array and do not want to return the element where it is true.
Thanks so much for this ;D
Thank you for reading it! Hope it helps :)
Loved the article! All of these array methods are very useful.
Thank you for your feedback :)
There is no requirement that the functions passed to map, filter etc. be anonymous or even arrow/lambdas. Well-named functions improve code quality (except in the most trivial cases).
Filter, some, every etc. expect "truthy" and not Boolean but using truthy instead of true in most cases is a hack which does not improve code quality:
You haven't defined what code quality means and I dare to disagree that these (very basic) array methods make the quality of your code better.
So maybe could you please elaborate on what code quality means and how exactly this type of methods makes the quality of the code higher?
Loved the article! All of these array methods are very useful. You can see more article like this on this website