Ever wondered what the JavaScript array map method is? what it can do? or when it should be used? This is article is for you. Before we go any furt...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for your article, the explanation is very clear.
On the other hand, I don't find the second example easy to grasp.
In modern programming, one of the most important principles is that elements should have names easy to understand and at the same time they should never lie about what their underlying structure does.
Let's have a look at it again:
The
map
function returns a new list calledoddSquareRoots
. So what do I expect to have in it? It's very simple! Odd square roots! What do I get instead?Those are not odd square roots! Those are some of the original numbers mixed with the square roots of some. Odd square roots apparently. Then I have a deeper look, I can figure out that actually, we have those original numbers that are even and the square roots of those that are odd. But that's not what the name says and that's something quite dangerous.
I don't work with JS a lot, so maybe there are different habits. Based on my experience, the example/use-case of applying a callback only on certain elements of a list is maybe a bit misleading.
Just like Python, JS also started to provide functional programming elements. Some of the main elements are
map
,filter
andreduce
. Each has its own very clear goal. I don't want to go into the very details, but in fact, they have very simple to understand names.In this case,
map
lies. What it does is filtering and then mapping some values and simply keeping some other values. Too many things in amap
by an unnamed function.If you want to get odd square roots, it's probably more readable to do it in two clear steps:
If you need those numbers in their original order, maybe
map
is the only option, but the output's name still lies.What do you think?
Thank you, Sandor, for pointing this out. That example was just to show that logic can be used in the map method. However, as you clearly pointed out, the filter method is a more efficient way of achieving that. In fact, my next post is on the filter method. I am going to update that example to add this point in order not to confuse anyone.
Great article. Thanks for writing this. I can definitely suggest this to someone who is learning Javascript and has questions on how .map() functions. It's also nice for getting a refresher on how .map() works.
Thank you Dave
On this code block you say that it: " ...finds the square of each element in the array..."
It would be more accurate to say that the relevant part of the code calculates (as opposed to finding) the square root of each element in the array.
Of course by the find, i think you meant "calculate" but then again, why not use the word calculate instead?
Great article. Thanks for share this post with us.
Great post. Very clear with sensible examples. The more I learn JavaScript the more I enjoy its power and short syntax.
Thank you Nicholas
Thank you for this. I now understand how the map function works in js
Totally awesome!, I love this
Thank you Runo
Awesome one, map methods have never been explained any clearer than this to me before. Thanks a lot Sarah.
Thank you Frank
Hi,
Thanks for sharing .It's really awesome.
But I have one question that in angular2 + why we are using with http get ,post etc ?
Thanks Sarah. I understand better now.
Thank you very much for your article
I was literally just thinking to myself, "I need to really understand how to use this." Thank you for explaining this!!!
Thank you for reading
Great post Sarah. I now clearly understand the map method. Can you blog about Javascript Events and their use in a map function on your next post?