All right.
In this video, let's talk about another method and that is map.
For this one tool, we have used it before, so let's just move fast here.
So come down here.
I'm going to be as map.
So map also loop through a record and display individual item.
As for each.
But the difference here is that, well, for the map, it creates a new array of which we can save it
into a variable.
And what we got, we can do any manipulation with it.
We can push a new record in it.
So that's not going to affect the original record.
So mostly if we want to take an array and do some mathematical operation or net, the map is the best
way to go because we're going to create a new array and a new array.
We can do whatever we want because it's going to affect the original array.
So for this one, let's say that I want to map through this to the end.
And then let's say when I got the students, I want to add different data into the record.
That's why maps comes into play.
So the same syntax for for age, we are going to do the same thing for map.
So because Map works on array, then what I'm going to do is that I would take the student's array on
that I have map and then here we go.
So inside as usual callback function and the same way we can pass in all the three agreements as we
did four for each well.
So for this particular function, we call it as high order function, because a function takes in other
functions, an argument is being regarded as a higher order function.
Great.
So what we have here is the actual user.
Now for map VERRASTRO we can console.log the user here.
So when I save it, check it out.
If we see that we have the same result as we did for for each.
The flexibility of using map comes here when we want to make a copy of the original array.
So in that case, instead of just console.log, we are going to return the.
Individual users here like that.
And then I'm going to assign to a variable cause.
Say new students like that new student is equal to that.
So for this one, for each loop map is going towards populate individual user into a new array for new
students variable as we see over there.
So with this one, if I console.log new student, you're going to see that I'm going to have an array
of new student and that is it.
So for this one, I can go ahead and say that, hey, now I want to push new student to the new student's
array.
So I'm going to take the new student dot push and just push.
One student's name is equal to Emmanuel and then the next age is equal to 31 years old.
When I save it now and check it out now instead of four, we have five record.
If I try to console.log the original array, let's see what we have that the students array.
Check it out.
The first one has five records.
This is the array that we copied and we push a new student into it.
And this is the original student's record.
We still have four students in it.
That is it.
And one good thing about MAP also is that we can retrieve certain parts of the user that we want.
So let's say that I in my application, I don't want to display, let's say the person's name and age,
but I only need the name only or age only.
So in this case, map shine over here too.
So in this one, if I want to retain the name, only then can I be user or student Dot's name.
Okay, so this one, if I have it, let's see if we see that we have what a race of names.
But the last one here is what I added as an object.
So in this case, because we are retrieving all name, you're going to have single values in the array.
Therefore, the way I'm pushing this data is going to be wrong.
So in this case I'm going to remove this one.
The here is that and even remove the object and the name here and just push in the name only.
And when I save it, let's see.
Now they have the same format.
That's a beauty of map.
So for a map two, we can change, like I said, for the newly array, we can change it in methods or
net.