Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,130 --> 00:00:05,550
Now related to arrays and objects,
2
00:00:05,550 --> 00:00:07,860
there are two crucial,
3
00:00:07,860 --> 00:00:11,292
relatively new and modern JavaScript features
4
00:00:11,292 --> 00:00:14,820
you should know because you will also see them quite a bit
5
00:00:14,820 --> 00:00:16,170
throughout the course.
6
00:00:16,170 --> 00:00:17,370
And the first feature
7
00:00:17,370 --> 00:00:21,033
is about destructuring arrays and objects.
8
00:00:21,870 --> 00:00:24,210
For that, let's take a look at this array.
9
00:00:24,210 --> 00:00:26,760
An array that contains some userNameData.
10
00:00:26,760 --> 00:00:28,890
The first name and the last name.
11
00:00:28,890 --> 00:00:31,020
Of course, it could be stored in an object
12
00:00:31,020 --> 00:00:32,970
but here it's an array.
13
00:00:32,970 --> 00:00:34,470
Now, let's say, in our code,
14
00:00:34,470 --> 00:00:37,323
we wanna work with both the first and the last name.
15
00:00:38,580 --> 00:00:40,326
Now what we could do to make it a bit easier
16
00:00:40,326 --> 00:00:42,630
is we could create a new constant
17
00:00:42,630 --> 00:00:45,041
or a variable called firstName
18
00:00:45,041 --> 00:00:48,600
where we extract the first element of this array,
19
00:00:48,600 --> 00:00:51,273
which is the first name, by using index zero.
20
00:00:52,410 --> 00:00:54,480
And then we could add a lastName
21
00:00:54,480 --> 00:00:56,880
where we also use userNameData
22
00:00:56,880 --> 00:01:00,900
and use index one to extract the lastName.
23
00:01:00,900 --> 00:01:01,890
This could be done.
24
00:01:01,890 --> 00:01:04,281
And from now on, we could use firstName and lastName
25
00:01:04,281 --> 00:01:06,955
which is a bit more meaningful than always working
26
00:01:06,955 --> 00:01:10,563
with userNameData zero and userNameData one.
27
00:01:11,610 --> 00:01:14,310
But this code can be shortened.
28
00:01:14,310 --> 00:01:17,640
Instead of using this code here,
29
00:01:17,640 --> 00:01:20,550
we could create these two constants
30
00:01:20,550 --> 00:01:23,132
or variables in one single step
31
00:01:23,132 --> 00:01:24,954
by adding square brackets here
32
00:01:24,954 --> 00:01:27,663
on the left side of this equal sign.
33
00:01:28,530 --> 00:01:32,280
On the right side, they will create a new array.
34
00:01:32,280 --> 00:01:33,540
On the left side,
35
00:01:33,540 --> 00:01:35,820
they will destructure that array,
36
00:01:35,820 --> 00:01:38,010
which simply means that this syntax
37
00:01:38,010 --> 00:01:42,780
can be used to pull values out of this array.
38
00:01:42,780 --> 00:01:45,360
And now you can define new constants
39
00:01:45,360 --> 00:01:48,660
or variables with any name of your choice here.
40
00:01:48,660 --> 00:01:51,060
And the first constant or variable here
41
00:01:51,060 --> 00:01:53,550
will be mapped to the first array element,
42
00:01:53,550 --> 00:01:57,243
the second one to the second element, and so on.
43
00:01:58,140 --> 00:02:01,860
And this is a super useful syntax since this, of course,
44
00:02:01,860 --> 00:02:05,310
is way shorter to write than what we had before.
45
00:02:05,310 --> 00:02:10,310
And from now on, I can also console.log firstName
46
00:02:11,520 --> 00:02:13,200
and console.log lastName,
47
00:02:13,200 --> 00:02:15,360
and use these constants like this
48
00:02:15,360 --> 00:02:19,020
even though the original data is an array.
49
00:02:19,020 --> 00:02:21,090
But due to restructuring,
50
00:02:21,090 --> 00:02:22,950
we can pull out these values
51
00:02:22,950 --> 00:02:26,790
and store them in separate constants or variables.
52
00:02:26,790 --> 00:02:29,880
So if I reload this page here,
53
00:02:29,880 --> 00:02:33,240
I see Max and Schwarzmuller here in the console
54
00:02:33,240 --> 00:02:36,510
because of these two constants that are created with help
55
00:02:36,510 --> 00:02:38,313
of this destructuring syntax.
56
00:02:39,270 --> 00:02:43,530
Now, this destructuring syntax also exists for objects.
57
00:02:43,530 --> 00:02:45,813
We cannot just use it for arrays.
58
00:02:46,950 --> 00:02:49,440
For that, let's say we had a user object
59
00:02:49,440 --> 00:02:51,300
that looks like this.
60
00:02:51,300 --> 00:02:53,970
Now again, we might want to store these values
61
00:02:53,970 --> 00:02:56,790
in separate constants or variables.
62
00:02:56,790 --> 00:02:58,990
For that, of course, we could create a name constant
63
00:02:58,990 --> 00:03:02,280
and store user.name inside of it
64
00:03:02,280 --> 00:03:05,550
and an age constant and store user.age inside of it.
65
00:03:05,550 --> 00:03:10,550
That would work, but again, this can be shortened here.
66
00:03:11,310 --> 00:03:13,906
Instead of writing this code,
67
00:03:13,906 --> 00:03:16,320
we can again use destructuring.
68
00:03:16,320 --> 00:03:19,020
In this case, since we're dealing with an object
69
00:03:19,020 --> 00:03:23,940
by writing curly braces on the left side of the equal sign.
70
00:03:23,940 --> 00:03:27,870
On the right side, they will be used to create an object.
71
00:03:27,870 --> 00:03:31,410
On the left side, they are used for destructuring.
72
00:03:31,410 --> 00:03:33,330
Just as we used square brackets
73
00:03:33,330 --> 00:03:37,080
on the left side for destructuring an array before,
74
00:03:37,080 --> 00:03:40,683
now we're using curly braces to destructure an object.
75
00:03:41,640 --> 00:03:44,910
And then here we can pull out name and age.
76
00:03:44,910 --> 00:03:48,270
Though, these names here are now not up to us.
77
00:03:48,270 --> 00:03:50,164
Instead, we have to use the field names
78
00:03:50,164 --> 00:03:52,443
that are defined in the object.
79
00:03:53,550 --> 00:03:55,230
For array structuring,
80
00:03:55,230 --> 00:03:58,080
these names were up to you because the elements
81
00:03:58,080 --> 00:04:00,123
were pulled out by position.
82
00:04:00,960 --> 00:04:01,860
For the object,
83
00:04:01,860 --> 00:04:05,760
they are pulled out by name, so by property name.
84
00:04:05,760 --> 00:04:08,710
And therefore you have to use the same property names here.
85
00:04:09,840 --> 00:04:11,949
Though, you could assign an alias
86
00:04:11,949 --> 00:04:13,890
by using a colon here,
87
00:04:13,890 --> 00:04:15,900
and then defining your alias name
88
00:04:15,900 --> 00:04:17,553
on the right side of that colon.
89
00:04:18,839 --> 00:04:19,971
When creating an object,
90
00:04:19,971 --> 00:04:21,810
so when using the curly braces
91
00:04:21,810 --> 00:04:23,640
on the right side of the equal sign,
92
00:04:23,640 --> 00:04:27,540
the colon is used to separate the key of a value,
93
00:04:27,540 --> 00:04:29,190
the property name, you could say,
94
00:04:29,190 --> 00:04:32,103
from the value that's stored under that property.
95
00:04:33,360 --> 00:04:34,505
When using curly braces
96
00:04:34,505 --> 00:04:37,170
on the left side of this equal sign,
97
00:04:37,170 --> 00:04:39,150
so when using them for destructuring,
98
00:04:39,150 --> 00:04:40,740
the colon is instead used
99
00:04:40,740 --> 00:04:43,867
for separating the property that's pulled out of this object
100
00:04:43,867 --> 00:04:46,863
from the alias name that should be assigned to it.
101
00:04:48,960 --> 00:04:52,660
And with that here, we could output userName and age,
102
00:04:55,080 --> 00:04:58,563
and we would be using these destructured values for that.
103
00:04:59,970 --> 00:05:02,430
So therefore, again, if we reload this,
104
00:05:02,430 --> 00:05:04,413
we would see these values here.
105
00:05:05,340 --> 00:05:07,440
And this destructuring syntax
106
00:05:07,440 --> 00:05:09,622
really is a crucial JavaScript feature
107
00:05:09,622 --> 00:05:12,030
which we'll use a lot throughout this course.
108
00:05:12,030 --> 00:05:14,193
And therefore now, you know about it.
8402
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.