Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:04,340
So we learned about rest and spread.
2
00:00:04,350 --> 00:00:11,110
There's one other next generation feature I definitely want to cover and that's called destructuring.
3
00:00:11,250 --> 00:00:18,070
Destructuring allows you to easily extract array elements or object properties and store them in variables.
4
00:00:18,180 --> 00:00:23,460
When you first hear about that it might sound like the exact thing the spread operator does but actually
5
00:00:23,460 --> 00:00:24,840
it's a different thing.
6
00:00:24,890 --> 00:00:32,299
Spread takes out all elements all properties and distributes them in a new array or object or wherever
7
00:00:32,299 --> 00:00:33,630
you're using it.
8
00:00:33,810 --> 00:00:41,820
Destructuring allows you to pull out single elements or properties and store them in variables for arrays
9
00:00:42,270 --> 00:00:43,820
and objects.
10
00:00:43,950 --> 00:00:46,130
Now for arrays it looks like this.
11
00:00:46,140 --> 00:00:48,050
If we have an array with two elements.
12
00:00:48,130 --> 00:00:54,900
Hello and max then we can use these strange looking syntax on the left side of the equals sign which
13
00:00:54,900 --> 00:01:03,480
looks like we are creating an array but we are not to assign the variables a and b to a Hello and Max
14
00:01:03,480 --> 00:01:12,630
respectively and for object destructuring it's the same syntax with curly braces and where in array destructuring
15
00:01:12,640 --> 00:01:17,090
the order defines which property we take, for object destructuring.
16
00:01:17,130 --> 00:01:24,180
It's the property name so curly brace name curly brace on the left side targets the name property of the
17
00:01:24,180 --> 00:01:30,150
object on the right side and pulls out the value which is why logging age would yield undefined.
18
00:01:30,180 --> 00:01:33,120
We're not pulling this out of the object here.
19
00:01:33,120 --> 00:01:34,730
So this is destructuring.
20
00:01:34,830 --> 00:01:42,280
Let's also have a look at this in practice back in jsbin,let's start with our numbers array again
21
00:01:42,580 --> 00:01:46,430
which has the numbers 1 2 and 3.
22
00:01:46,630 --> 00:01:49,450
And now let's say we want to get the numbers 1 and 2.
23
00:01:49,840 --> 00:01:55,750
Then we can use this array syntax on the left side of the equal sign and choose any variable names of
24
00:01:55,810 --> 00:02:02,980
our choice like num1 and num2 and we assign this equal to the number as array.
25
00:02:03,380 --> 00:02:12,200
If I now console log num1 and num2 and I run this you see 1 and 2 being logged because I'm
26
00:02:12,200 --> 00:02:14,890
pulling these two out of the array.
27
00:02:14,900 --> 00:02:18,050
I'm not pulling 3 if I want to get three.
28
00:02:18,060 --> 00:02:21,130
By the way I'll simply add num3 here.
29
00:02:21,170 --> 00:02:27,740
You don't have to rename it though and you'll leave out the one space with an empty space here in your
30
00:02:28,340 --> 00:02:30,140
declaration on the left side.
31
00:02:30,140 --> 00:02:33,630
Now if you had run you get one and three.
32
00:02:33,650 --> 00:02:35,710
That's array destructuring.
33
00:02:35,750 --> 00:02:42,910
Now object destructuring is also supported by our project as we'll use it in this course.
34
00:02:42,920 --> 00:02:46,250
However it's not supported by jsbin here.
35
00:02:46,250 --> 00:02:47,750
So I can't show it to you.
36
00:02:47,780 --> 00:02:50,320
But I did show the syntax on the slide.
37
00:02:50,480 --> 00:02:56,600
It's the syntax where you use curly braces on the left and target a property with its property
38
00:02:56,600 --> 00:02:57,170
name.
39
00:02:57,170 --> 00:03:03,620
In this course I won't use these destructuring syntaxes too often but I want you to be aware of the existence
40
00:03:03,620 --> 00:03:09,800
of them and that they are nice ways of conveniently pulling out certain elements from an array or properties
41
00:03:09,800 --> 00:03:11,840
with their values from an object.
4227
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.