WEBVTT

00:01.270 --> 00:06.550
Let's talk about another era helper method, which is map, as it was in case the forage method, I'm

00:06.550 --> 00:08.640
going to compare map helper to follow.

00:10.600 --> 00:17.740
Suppose that we have an array which consists of some numbers, so create variable numbers and insert

00:17.740 --> 00:21.760
here numbers from, for example, one to five.

00:23.930 --> 00:30.260
Then suppose that we want to multiply each of these numbers to ten, keep each iteration and result

00:30.260 --> 00:33.080
in a new era without changing the original one.

00:33.800 --> 00:36.980
So let's create an empty array and call it new array.

00:39.580 --> 00:48.100
And then let's use for a loop to iterate through the numbers, right, for I equals to zero, then I

00:48.100 --> 00:54.250
is less than the length of numbers array and then I plus plus.

00:55.630 --> 01:02.470
So in order to add results in new array, I'm going to use known array method called push, which adds

01:02.470 --> 01:04.630
new item into array as a last one.

01:05.050 --> 01:07.960
So right now you are right that push.

01:09.790 --> 01:19.420
And inside the parenthesis right numbers, I multiplied to 10, as we said, we want to create new array

01:19.420 --> 01:22.400
and keep the original one at the same time.

01:22.780 --> 01:24.850
So let's look both of them in cancel.

01:24.880 --> 01:25.270
Right.

01:25.270 --> 01:26.200
Cancel the log.

01:27.460 --> 01:27.970
No.

01:29.840 --> 01:32.090
And then consider that look, New Arite.

01:35.490 --> 01:36.050
Reload.

01:36.180 --> 01:40.830
And here we go, we have original array and the new era with new items.

01:41.940 --> 01:42.360
All right.

01:42.750 --> 01:48.420
It's one of the ways to reach this results, but we can do the same, but in a much simpler and easier

01:48.420 --> 01:48.630
way.

01:48.630 --> 01:52.200
Using Map Hopper, let's again make here some border.

01:55.070 --> 02:03.020
I'm going to create a new variable and store the results into this variable, so let's call it map array.

02:04.590 --> 02:09.630
Which equals to numbers and use here MUP Harper.

02:11.020 --> 02:15.640
Syntax is very similar to forage method, we need to pursue an anonymous function.

02:17.950 --> 02:23.560
And then let's pass as a parameter number in singular form, as it was previously.

02:26.030 --> 02:31.250
All right, it's very important to remember that always when we deal with error happens, we need to

02:31.250 --> 02:32.570
use a statement.

02:33.090 --> 02:35.510
The right return, no.

02:37.080 --> 02:38.550
Multiplied to 10.

02:42.420 --> 02:46.440
Let's say in council, what we have looked at first, the original numbers are right

02:49.260 --> 02:52.950
and then newly created variable, not a rate.

02:56.210 --> 02:56.720
Revote.

02:57.230 --> 03:03.290
And here we go, we have the exact same result as it was in case of Falbe, but in this case, you notice

03:03.290 --> 03:07.160
very clearly that the amount of code is much less than in previous case.

03:07.700 --> 03:10.160
We got the same result in much more simple way.

03:10.160 --> 03:16.220
And the most important thing to note is that you realize at a glance that logic is much more simple

03:16.220 --> 03:17.120
to understand.

03:18.560 --> 03:23.960
Again, you must keep in mind always to use a written statement, because if we miss it.

03:24.930 --> 03:27.900
Then the function will give us the values as undefined.

03:30.330 --> 03:34.620
All right, let's take a look on how map Harper actually works behind the scenes.

03:35.580 --> 03:42.390
Here we have our original array of numbers, then another array which is created using Map Harper and

03:42.390 --> 03:43.320
the function itself.

03:44.100 --> 03:50.910
So at first function is executed for first item one is multiplied to ten and the result is added to

03:50.910 --> 03:51.780
an empty array.

03:52.710 --> 03:58.560
Then function is executed for second item, which is two, it's multiplied to ten and the result is

03:58.560 --> 03:59.960
added two new array as well.

04:00.780 --> 04:09.960
And in the same way function is executed for the item, then for fourth item and for last item, which

04:09.960 --> 04:10.530
is five.

04:11.160 --> 04:15.010
And all the results are added to a new array again.

04:15.030 --> 04:21.210
Remember that when we use Matara Helper, the original array is maintained as immutable, which means

04:21.210 --> 04:22.350
that it's not changed.

04:23.600 --> 04:24.230
Makes sense.

04:25.010 --> 04:25.460
All right.

04:26.460 --> 04:32.760
Let's writes one more example, suppose that we have an array of persons which consists of several objects

04:32.760 --> 04:42.420
with properties, force them in last name, so create variable persons, then insert in its object with

04:42.420 --> 04:42.930
properties.

04:42.930 --> 04:44.640
First name John

04:47.280 --> 04:48.570
and last name Smith.

04:50.730 --> 04:56.130
Let's duplicate it twice and then change the values of properties, writes Bob.

04:58.430 --> 04:59.270
Then Brown.

05:02.620 --> 05:05.860
And the third one, Nick and Dought.

05:08.640 --> 05:14.120
Now, suppose that we want to group first names in you arrange for that, the perfect way is to use

05:14.420 --> 05:15.230
a right helper.

05:15.600 --> 05:18.990
So let's create a variable, get first names.

05:21.300 --> 05:29.250
Which equals to persons that Matt Puscifer, again, a function with parameter.

05:30.380 --> 05:30.980
Person.

05:34.370 --> 05:40.460
As we said, we are going to group first names of those objects, so we need to return the person that

05:40.880 --> 05:41.600
first name.

05:45.340 --> 05:48.820
So everything is ready, let's make again here little line.

05:53.700 --> 05:59.190
And then, look, both variables in console, right, console the clock persons.

06:00.790 --> 06:03.970
And then cancel the luck, get first names.

06:06.350 --> 06:06.910
Reload.

06:07.910 --> 06:13.460
And here we have our original persons array and then you array, which consists of first names of the

06:13.460 --> 06:19.700
objects, you can try the same with follow and you will be ensured that map is much more convenient

06:19.700 --> 06:23.840
and fast way to set up the same logic and get the same result.

06:24.920 --> 06:26.600
All right, let's go ahead.
