JavaScript Map() - Explained!
37K views
Dec 12, 2022
The JavaScript map() function in JavaScript is a method that allows you to transform every element in an array into a new element, based on a provided function. This is a useful tool for creating a new array from an existing one, and can be used to perform a variety of operations on the data contained within an array. Article: https://www.ceos3c.com/javascript/javascript-map/
View Video Transcript
0:00
Hey guys, what's up everyone? Welcome to a new video. In today's video I'm going to tell you more about the JavaScript map function
0:07
As you guys know, we always provide a written tutorial that goes along with our videos
0:11
I will leave the link to that written tutorial in the video description below. Alternatively, you can just go to seosec.com, click on learn and then on JavaScript tutorials
0:20
And in there you will find all of our JavaScript tutorials. And the one we are going to look at today is
0:27
JavaScript map and if you open up that article which we highly recommend
0:32
because that makes it much easier for you to follow along then you can see
0:36
that there is a replit repository attached to this article that you can
0:41
open by clicking on fork repl and this opens up the code that we are going to
0:46
write during this video which makes it very easy for you guys to follow along
0:51
and play around with so we highly recommend you guys pulling up that article while we are going through the video also guys if you haven't subscribed yet to this channel
1:00
please do so this is a brand new channel we created this channel to make learning more interactive for
1:04
you so that we can link together all of the videos that we provide to our blog articles which
1:10
hopefully results in a better learning experience for you guys let us know what you think about this
1:15
approach in the comments of this video below with that being said guys let's jump right into
1:19
JavaScript map now the JavaScript map function in JavaScript is a method that
1:24
allows you to transform every element in an array into a new element based on a
1:29
provided function this is a useful tool for creating a new array from an existing
1:35
one and can be used to perform a variety of operations on the data contained
1:39
within an array now let's look at some examples to demonstrate you how the
1:44
the JavaScript map function works let's look at a simple example here in this
1:50
example this map function is used to square each element in an array so let's
1:55
say we have an array with const numbers and we have to make the screen a lot
2:00
bigger for you guys to see that properly and again this is replit if you want to
2:05
check that out you can click on the link and we always like to keep our exercises
2:09
interactive and this allows you to code along right with us without setting up
2:13
any development environment or such. Okay, so this numbers array, we just say it's a couple of numbers in there one two three
2:21
four five and Then when we go ahead and create the map function the default or the syntax for this map function is const
2:30
Then you do a function name So let's say squared numbers or this actually the variable where we are going to store the output of our map function in
2:37
So we say const squared numbers equals numbers and now we call the map function which is numbers
2:44
So you call the map function on the numbers array? So it's numbers dot map then you open up some parentheses and inside of that
2:53
We just say n for the number that it's looping over so this n represents each element of this array
3:00
We can also call this item or whatever we want. It doesn't matter. It's just a placeholder name and
3:04
and then we do a arrow function here, and then we do n times n, so number times number
3:13
And once we have that set up, we can then go ahead and just console.log the output of this function
3:19
and we want to get the squared numbers, because that's where our numbers are stored in
3:25
Now, if we run that, that should give us the square of each element
3:29
Let's check it, if it's actually working. Might take a couple of seconds here because I'm running this first time
3:35
Okay, there we go. So we see 1 of course is still 1 and then 4 is doubled and we have the square of 3 which is 9 and 4 is 16 and 5 is 25 and so on So you can see the syntax of the map function is pretty pretty easy and this is just to show you how the syntax looks like Now in this code the map function is called on the numbers array as we seen here and
4:01
is provided with a callback function that squares each element in the array. The resulting array squared numbers contains the squared
4:10
values of each element in the numbers array just to summarize that again
4:15
Now we can also use the JavaScript map function on an array of objects
4:21
This is what a lot of you guys always ask us Can you also use that on an array of objects and the answer is yes, you can
4:27
So the map function is a powerful tool for transforming arrays and it can be used in a variety of different ways
4:33
For example, you can use it to create a new array of objects based on the elements of an existing array
4:39
Now what does that mean? Let's look at it. Let's say we have an array of objects
4:44
We say users equals to an array of objects, so we open that up and let's just say it's an ID here, ID1 and a name
4:56
Say John. Okay, so we have an array of objects. Now we have just one object
5:01
Let's make it a bunch more so I'm gonna just copy and paste that copy
5:06
Next one say we make Four of them, so we're gonna change the IDs respectively three four and we also change the names up a bit
5:17
John Jane Bob Sally Or Sully however where you're located that might your mileage may vary. Okay
5:31
So now we have an array of objects here And if we now go ahead and again do create a variable let's say names and equal that to
5:40
Now users dot map again We call the map function on the users array of objects this time and we say you for user arrow function user dot name
5:51
Now what we are going to do here. Let's see. Let's just console dot lock that
5:56
Let's console unlock the names The names variable and see what it says what do you expect it will say
6:06
So as you can see it basically pulls out all of the names that are present in the users
6:12
Array of objects and it puts it in a completely new array called names
6:17
So you might now get an idea of why this is useful when you are writing JavaScript code now again
6:24
Let's summarize in this code the map function is used to create a new array called names. It's this one here
6:31
Containing the name property of each object in the users array. So it's looping over the users
6:37
array of objects here and on each of those objects this is the u represents one of each of those and then we say we want
6:47
the u.name property here so we accessing this and then it's going to push it
6:52
basically into a new brand new array for us where only the names of this you
6:57
array of objects is contained in okay so this is the next thing we can look at
7:04
pretty pretty interesting you can use that for many many different use cases
7:08
Now can you map an object in a JavaScript is another question we get a lot
7:14
The answer to that is yes You can map an object in JavaScript by using the object dot entries method to convert the object into an array of key value pairs first
7:24
And then using the map function to transform the array There is no built map function to use with JavaScript object So this is kind of a workaround now Let demonstrate What I mean by that Let create an object We call it const user obj just like this user object short for or object short for object
7:43
Then we do ID one Let's give it a name say Jane. Okay
7:49
Just to represent the object here and let's say age 30 So we have two numbers here in one string
7:54
now we are going to apply some logic to this that you can actually see that we
7:58
can do something with the result of that so I'm going to just write a function
8:03
that basically doubles the the numbers in this object so we say we double this
8:10
number and we double this number of course we cannot double the name it's just a mere demonstration of how this function works now let's write our
8:18
function let's say const update user in case we want to modify the data right so
8:23
So we do object entries and then we open that up and then we do pass in the user object
8:30
that we have up there and then we do dot map and now it gets a little bit dicey here
8:39
If you are new to all of this or if you're new to programming, this can look quite a bit confusing
8:45
So we pass in some logic here and that is, we need to first create a key value pair
8:53
I'm going to go in detail over this function what it does after we wrote it out
8:58
So we enter a key and a value then we do an arrow function and then we say
9:06
Let's do something with that. So we say key and value times two and
9:12
Then we need to chain the reduce function on it and again
9:16
I explained to you what that does in a second and we say the object and and the
9:23
key and value Another arrow function here and then we say Dot dot dot. It's the three dots method. Now. Don't worry about this is just to put again together this object and we do object
9:37
comma and then the pass in the key and Then a colon and then value
9:45
We have to close that off and then we need to add a comma on the right spot and pass in an empty
9:53
object again very Not easy to understand. I get it but I just want you guys to know that it's possible to actually
10:01
kind of use the map function on an object and Let's just console.log the resultant. It's already written here because I just tried it out
10:10
So we do console.log and we do update user So let's run this and you already were able to see the output anyway
10:20
Nevertheless, let's run it. So as you can see it actually doubled the user ID
10:24
It couldn't double the name because it's a string and not a number and it's at the age also to double value
10:30
Which is 60 in this case now how all of that actually works
10:35
So in this code the object entries method, which is this one
10:40
Is used to convert the user object, which is this one into an array of key value pairs, which is this key and value
10:51
Now, this array is then passed to the map function, that's what we do here
10:56
which doubles each value in the array, which we do here. And then finally, the reduce method is used to convert the resulting array back into an object
11:09
So with this reduce function we convert the result back into an object which is represented here So we basically did a two conversion first we put the object into a iterable array and then we put this array back into a object now keep in
11:30
mind that this approach will only work for objects that have simple data types like numbers and strings as values if the object contains nested object the
11:38
objects or arrays you will need to use a more complex approach to the map to map
11:43
the object now there is also a little bit of an easier way to write this in a
11:48
one-liner so we can do the same thing let's say let's leave that at user
11:53
object and remove this logic here and then you can just do let new object
11:59
equals object dot from entry so we use the from entries method here we say
12:06
object dot entries then we pass in the user object and then we do a map
12:13
Now this gets even this is probably more hard to understand than the previous one the previous one is the broken down function
12:22
This is the one-liner that we can use to do the same thing
12:25
So we do map and then we shorten things a little bit
12:30
So instead of key and value we just do K and V for key and value and we basically do the same thing
12:35
We do an error function and then we say K comma V key and value times V and
12:41
And then we close that off. I think That should do We are missing something somewhere as always
12:52
Let's see this looks pretty good. Okay, this should work and Let's see console lock new object
13:02
New object, let's see if it actually works. I think I missed something somewhere
13:06
So as if you write too many parentheses parentheses, yep type error there we go
13:12
I knew it so I'm gonna just copy and paste that from the article. There we go. Yeah, I missed one
13:17
One parentheses now. Let's run it. Oh We have the wrong object in here probably I call it different in the article. Yep, there is user object
13:30
Sorry for that now if we run it, it should work There we go. Now it returns again the ID and the age is times 900 because now we are multiplying
13:41
it by the value itself and not just times 2. I think before we did times 2. Let's see if we
13:46
change that to times 2 and there we go. We have ID 2 and age 60. So this is basically the same
13:52
thing just in a one-liner and a little bit shorter. I know this is a pretty complicated
13:57
topic to wrap your head around if you're new to JavaScript but the JavaScript map function is a
14:02
and versatile tool for transforming arrays in JavaScript. It allows you to easily create new arrays based on the elements of existing arrays
14:10
and can be used to perform a wide variety of operations on array data
14:14
And, yeah, I guess practice a little bit with it. Definitely open up the Repl.it repository and play around with it yourself
14:21
You get the hang of it once you work more with arrays. You definitely need the map function all the time if you work in a programming gig
14:29
And I hope this video helps if it did guys please make sure to subscribe to the channel once again
14:35
We need to get subscribers up on this channel and also check out the blog for regular updates. Thanks for watching
14:40
I hope to see you back in the next one until then