This post is a quick note to underscore how important it is for devs to get comfortable with documentation. Mind you, I'm the first one over on Sta...
For further actions, you may consider blocking this person and/or reporting abuse
You can do something more to keep this code structure, just a helper variable:
But to keep the functional style that you want with
forEach
and stay in simple(readable?) code:Sometimes exists another method in the docs to get the goal that you want...
Thank you!
some method doesn't check the condition with every element present in the array it just returns true if one element satisfies the condition.
Like in above code some method only checks the condition with first element in the array so it returns true .
There is a every method in javascript, it checks the condition with every element present in the array.
References
If we break down the code, it is easier to see what is going on.
I'm gonna pull the function out of the
forEach
and create a newgte
function.Array.forEach
's first argument is a function. I would recommend always breaking the function out to make the code more understandable.When written this way, it is more clear what is going on and that you would not expect the
forEach
to return a value from the outer functiongreaterThan
.I also agree with kip, the better way would be
some
orevery
since it will break early on afalse
and not iterate the entire array.Thank you so much for this!
Shout out to the ReasonML and ReasonReact docs.
I built my entire project from blank file to production, starting with zero knowledge of Reason, without needing any resource at all outside of these two websites once. That's not something that happens too often.
I think it's helpful to understand why, regardless of what the
forEach
method actually does, the above couldn't work.Even if we imagine that
forEach
short circuits on a truthy return value from the function passed in... and returns that value...There's still no way that value would make it to the output of our
greaterThan
function.This would work (if that's what
forEach
did), since we are taking note of the return value. (The|| false
is there so that we know exactly what we're returning, notnull
orundefined
)Since the
.findIndex()
.find()
.some()
family of methods do exist, we can indeed do that:Functions that have only one statement - a return statement, are also prime candidates to be arrow functions:
Finally, if doing this in real life, you probably want to avoid iterating on every check if the array is unchanged:
I see all the solutions in the comments, you can also do this with the filter method:
OR (basically the same)
Array.prototype.filter()
is probably the least suitable here, for two reasons:1) It allocates an output array.
2) Even if the very first element matches, it will still test all of the remaining elements.
In contrast,
Array.prototype.some()
only returns a boolean, and never executes the predicate past the first match. In other words, it implements the optimizations one would do in thefor
loop when does at least something match? is the question.If you do care about the value, there's
Array.prototype.find()
, and if you actually need the key/index yourself, for example to replace the value in place or look up a matching index in another array, there'sArray.prototype.findIndex()
Wondering why you didn't use
[12,3,44,5].find
function for the same ?In the example
find
is the solution that is the best description for the wanted behavior.In general, if you "wanted perform some calculations on each item of an array", prefer
map
(a pure transformation of each object) over imperativefor
andforEach
.Totally agree . People who start programming, generally they start with the procedural paradigm of programming.
for loop
is one the first thing that people come across (along with if-else). That sticks to their mind forever , until functional programming is introduced to them . Its just matter of time to get used to such paradigm . I am pretty sure with few weeks, the author of the article will be very well versed in functional programmingWhat I'm loving about this thread is reading all the ways to accomplish my coding goal π Thanks so much for your suggestion.
Because of this reason I joined dev.to recently .
Back to the arrays discussion, the trend of using for loop will go away.
For every use case there must be some array function available.
w3schools.com/jsref/jsref_obj_arra...
And one thing to remember for interviews is that none of them returns a new array ( or iterable ) only
Array.prototype.map()
returns a new array or iterablethis has what confused me a lot in few of my interviews from those harsh 73 interviews.
filter
returns a new array as well.And in cases where it's used as a map+filter optimization, so does
reduce
(but not in other cases).can you please elaborate on this more or give some references articles/blogs to read from?
Idk, they're really easy to find, I don't have a specific source.
This seems adequate enough: medium.com/jsguru/javascript-funct...
When I first started out using Angular, I developed a bias for its official documentation as it was very comprehensive.
Agree, I love how angular's docs written. The problem is Angular tries to cover a much broader set of problems, hence its overall harder for people and in the end, less popular.
This was originally in perldoc. I think it applies very generally if you take out the Perl-specific vocabulary:
I like to think of it as
forEach
not returning a value at all, whether or not that's true. That helps to remind me that it's purely for side effects.I usually refer to docs when I forget how a specific function works, or if I need to explore what certain objects can do.
This makes me appreciate even more the work a community puts into getting very good documentations in place. Eg: Vue.js, Laravel, etc.
I thought this tweet was pretty on-point
That tweet somes up my life so far π