WEBVTT

00:06.370 --> 00:11.870
In this video you're going to learn how to create arrays from other objects in E6.

00:11.920 --> 00:19.150
So in iOS 6 you can create an array from objects which are similar to arrays like strings collections

00:19.180 --> 00:20.530
and so on.

00:20.530 --> 00:27.430
So these objects they are simulated arrays because they have a length property and they may also have

00:27.580 --> 00:30.280
elements indexing or they may not.

00:30.280 --> 00:31.040
All right.

00:31.240 --> 00:37.360
So first let's understand why you would need to create arrays from such objects.

00:37.360 --> 00:42.700
And then after that you're going to have a few examples where we can create arrays from collections

00:42.760 --> 00:45.550
strings functions and so on.

00:45.550 --> 00:50.940
So you're going to have a few essential elements which you're gonna use later.

00:51.010 --> 00:55.870
So you've got you've got a few divs here with a class Blue which you're going to use at some point to

00:55.870 --> 00:57.430
create a collection.

00:57.430 --> 01:05.520
So for now let's understand why we need to create arrays from other objects so let's create an array.

01:05.550 --> 01:06.070
Distance

01:12.010 --> 01:20.680
and let's explore some of the methods of the Array objects that you wouldn't find in a collection or

01:20.680 --> 01:22.360
a string for example.

01:22.390 --> 01:24.990
So a good example of that is the map function.

01:25.630 --> 01:31.650
So you can create a new array out of this array using the map function.

01:31.690 --> 01:38.580
And if I remember you pass a function to the map function or the map method.

01:38.670 --> 01:48.870
So let's say that we cannot pass a function that returns the or X multiplied by two.

01:48.870 --> 01:50.830
And now you get to 4 and 6.

01:50.830 --> 01:57.730
So we took that array we used the map method and we applied this function to the elements of the array

01:58.120 --> 02:01.410
and we created a new array.

02:01.420 --> 02:11.780
Now if you Troy the map method with a string for example unfortunately it is not gonna work.

02:11.830 --> 02:24.550
So if you log X dots map you get undefined meaning that map is not attached to a string but if you log

02:25.960 --> 02:32.660
the array dots map you'll get a function because the function does exists for an array.

02:33.460 --> 02:41.230
So to use the map method with a string in year five we had to do some really laborious work we had to

02:41.230 --> 02:52.900
go back through the array prototype like this array dot prototype and then we used the map method like

02:52.900 --> 02:58.570
that's why using the call function.

02:58.570 --> 03:05.960
So this function takes are they first parameter the value you want for this which is gonna be your string.

03:06.230 --> 03:06.860
All right.

03:06.860 --> 03:11.690
And so let's call this drink you are just to avoid any confusion

03:16.420 --> 03:22.690
and then you specify the arguments of your map method.

03:22.780 --> 03:25.090
So let's say that we want a function

03:28.590 --> 03:30.690
okay so I'm not gonna use an error function.

03:30.690 --> 03:34.090
I'm gonna use a simple function here because we said back to iOS 5.

03:34.570 --> 03:39.690
So while you can still use a narrow function that's absolutely fine.

03:39.970 --> 03:43.420
I'm gonna go for this syntax for now the S5 seems likes.

03:43.640 --> 03:57.720
So let's say that s the function takes a parameter X and returns x plus a for example all right.

03:57.790 --> 04:00.220
And now you get a new string.

04:01.740 --> 04:04.380
So that's really laborious.

04:04.380 --> 04:05.020
All right.

04:05.070 --> 04:12.630
So in E6 instead of writing all that code you can just convert the string to an array and then use the

04:12.690 --> 04:14.440
map method if you like.

04:14.580 --> 04:14.850
All right.

04:15.180 --> 04:28.090
So to convert an array to a string you just use the from method of the array object to which you parse

04:28.240 --> 04:30.010
the string.

04:30.010 --> 04:37.420
So now if you do that you're gonna get a new array created from this string.

04:37.420 --> 04:42.970
So now you can use all sorts of methods which are attached to the array prototype so you can use the

04:43.000 --> 04:47.490
mop method you can use the sliced method and so on.

04:47.720 --> 04:48.230
Right.

04:48.430 --> 05:00.230
So let's go back now two years five and understand why we we still need the to create arrays from other

05:00.390 --> 05:02.270
these are types like collections.

05:02.960 --> 05:10.060
So here we've got three lives of class blue.

05:10.190 --> 05:14.340
So what we're gonna do we can create a

05:18.610 --> 05:28.770
collection of these elements by selecting all elements of class blue so you can use a concept here instead

05:28.780 --> 05:36.870
of let's not really because we changed array afterwards.

05:36.920 --> 05:37.890
That's fine as well.

05:37.970 --> 05:51.170
So in our collection we're gonna have all the elements of class blue.

05:51.670 --> 05:58.670
So if your log collection you get in a shameful collection.

05:59.080 --> 06:06.970
So now let's say that you want to get only one portion of the collection.

06:07.390 --> 06:16.330
For example only the elements of index one of the elements of index to know if you had an array you

06:16.330 --> 06:18.630
could do thus using the slice method.

06:19.030 --> 06:25.810
But the slice method doesn't work with a collection to be try that you get undefined.

06:25.810 --> 06:29.410
There is no slice method attached to a collection.

06:29.410 --> 06:34.150
So again to do that in year five

06:38.240 --> 06:44.760
you had to use the slice method attached to the array prototype.

06:46.380 --> 06:46.630
Okay

06:51.790 --> 06:52.300
like this

06:55.230 --> 07:00.640
and then you had to use the call method specify the value of this which is gonna be your collection

07:01.840 --> 07:04.930
and then you parse the arguments of these slice function here.

07:04.930 --> 07:09.590
So we want to go from the elements of indexed one to the elements of index 2.

07:09.700 --> 07:15.190
So that's going to be from 1 to 3.

07:15.190 --> 07:15.440
All right.

07:15.460 --> 07:23.450
Because this will go from the elements 1 until 3 3 not included.

07:23.670 --> 07:23.890
All right.

07:23.890 --> 07:25.140
So now if we try these

07:28.640 --> 07:37.860
you will get an array this time and you get only the first or the elements of index 1 and the element

07:37.860 --> 07:39.270
of index 2.

07:39.540 --> 07:47.070
Now in iOS 6 you can convert the collection to an array and then use this like method.

07:47.190 --> 07:47.820
All right.

07:47.910 --> 07:51.840
So to to convert the collection.

07:51.840 --> 07:59.400
So you just use the array objects from method and you pass the collection so it's like that.

07:59.440 --> 08:01.540
And this time you get an array.

08:01.590 --> 08:09.070
Now you can use the slice method as you like attach it to this element.

08:09.220 --> 08:09.500
All right.

08:09.500 --> 08:17.100
So this explains why you would need to create arrays from other objects which are similar to arrays.

08:17.110 --> 08:23.010
Now let's have a look at one more example creating an array from function arguments so it creates a

08:23.010 --> 08:30.470
function G which takes a couple of promises.

08:30.620 --> 08:33.470
So first let's log the arguments

08:38.910 --> 08:42.220
so let's call g of 1 and 2.

08:42.220 --> 08:49.060
So the arguments and arguments elements it's not really in the right and you can see all elements inside

08:49.060 --> 08:49.820
the arguments.

08:49.880 --> 08:53.110
You can see your parameters and so on.

08:53.530 --> 08:54.510
All right.

08:54.580 --> 09:04.510
So now if you want to convert the arguments to the right again you use great dots from

09:10.050 --> 09:17.040
right and you've got an array and now you can use all sorts of methods which attach to the array prototype.

09:17.150 --> 09:17.430
All right.

09:17.430 --> 09:22.950
So in the next video you're going to learn some other features of the array dots from method.
