Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
1
00:00:00.000 --> 00:00:04.000
So far you have
2
00:00:04.000 --> 00:00:08.000
learned about 3 kinds of loops in JavaScript. 4 loops,
3
00:00:08.000 --> 00:00:12.000
while loops and do-while loops.
4
00:00:12.000 --> 00:00:16.000
With all these loops we can repeat an action a number of times.
5
00:00:16.000 --> 00:00:20.000
But we have two more kinds of loops in JavaScript and we use them
6
00:00:20.000 --> 00:00:24.000
to iterate over the properties of an object or
7
00:00:24.000 --> 00:00:28.000
elements in an array. Let me show you. In this lecture, we're going to look at
8
00:00:28.000 --> 00:00:32.000
the for-in loop. So let's say we have an object like
9
00:00:32.000 --> 00:00:36.000
person, with two properties name,
10
00:00:36.000 --> 00:00:40.000
Mosh and age 30. Let's say
11
00:00:40.000 --> 00:00:44.000
we want to display all the properties of this person object.
12
00:00:44.000 --> 00:00:48.000
That's when we use the for in loop. So for,
13
00:00:48.000 --> 00:00:52.000
now in parenthesis, unlike the for loop that we learned about earlier,
14
00:00:52.000 --> 00:00:56.000
we don't have three parts, so we don't have that initial
15
00:00:56.000 --> 00:01:00.000
expression followed by a semi colon, condition
16
00:01:00.000 --> 00:01:04.000
followed by the increment part. It looks a little
17
00:01:04.000 --> 00:01:08.000
but different, let me show you. So we have let
18
00:01:08.000 --> 00:01:12.000
key in person. So in every iteration
19
00:01:12.000 --> 00:01:16.000
this key variable in the loop will hold the
20
00:01:16.000 --> 00:01:20.000
one of the properties in this person object, let me show you.
21
00:01:20.000 --> 00:01:24.000
So here I'm going to do a simple console.log of key,
22
00:01:24.000 --> 00:01:28.000
save the changes. So in the first iteration, key is
23
00:01:28.000 --> 00:01:32.000
name, and the second iteration it is age. Now what if you
24
00:01:32.000 --> 00:01:36.000
want to display the value of each property next to it. Well
25
00:01:36.000 --> 00:01:40.000
earlier that there are two ways to access the
26
00:01:40.000 --> 00:01:44.000
properties of an object. We can use the dot notation
27
00:01:44.000 --> 00:01:48.000
which looks like this, person.name, or
28
00:01:48.000 --> 00:01:52.000
we can use the bracket notation.
29
00:01:52.000 --> 00:01:56.000
So person square brackets, the name of the target
30
00:01:56.000 --> 00:02:00.000
property as a string. Earlier I told you, that we used
31
00:02:00.000 --> 00:02:04.000
the bracket notation when we don't know ahead of time, at the time of writing
32
00:02:04.000 --> 00:02:08.000
code, what property we're going to access. Perhaps the name
33
00:02:08.000 --> 00:02:12.000
of the target property is calculated at run time. Here is
34
00:02:12.000 --> 00:02:16.000
a real example. When we iterate over the properties
35
00:02:16.000 --> 00:02:20.000
of the person object, in each iteration the
36
00:02:20.000 --> 00:02:24.000
value key is going to be different. So here we can use the dot notation,
37
00:02:24.000 --> 00:02:28.000
to display the value of this property, in other words,
38
00:02:28.000 --> 00:02:32.000
we can't do something like this, because we don't have
39
00:02:32.000 --> 00:02:36.000
a property called key in the person object. So that's
40
00:02:36.000 --> 00:02:40.000
when we use the bracket notation. So we add square brackets,
41
00:02:40.000 --> 00:02:44.000
and pass key as the name of the target property.
42
00:02:44.000 --> 00:02:48.000
Now let me delete this stuff here. Save the changes
43
00:02:48.000 --> 00:02:52.000
so you can see the value of name is Mosh and the value of age
44
00:02:52.000 --> 00:02:56.000
is 30. So this is the for in loop. and we use it to
45
00:02:56.000 --> 00:03:00.000
iterate over the properties of an object. We can also use these
46
00:03:00.000 --> 00:03:04.000
to iterate over an array. But it's not an ideal way, in the next lecture
47
00:03:04.000 --> 00:03:08.000
I'm going to show you a better way, but let's see how that works before we finish this
48
00:03:08.000 --> 00:03:12.000
lecture. So I'm going to define an array called colors,
49
00:03:12.000 --> 00:03:16.000
with three values, red, green,
50
00:03:16.000 --> 00:03:20.000
and blue. Now we can use the for in
51
00:03:20.000 --> 00:03:24.000
loop to iterate over this array. So for let
52
00:03:24.000 --> 00:03:28.000
index in colors, note that I
53
00:03:28.000 --> 00:03:32.000
named this loop variable index, because in each iteration,
54
00:03:32.000 --> 00:03:36.000
this index variable will be set to the index
55
00:03:36.000 --> 00:03:40.000
of one of the elements in this array. So it's going to be 0
56
00:03:40.000 --> 00:03:44.000
1 and 2. Let's take a look. So console.log
57
00:03:44.000 --> 00:03:48.000
index. Save the changes. So,
58
00:03:48.000 --> 00:03:52.000
we get 0 1, and 2. Now if you want to get the elements
59
00:03:52.000 --> 00:03:56.000
of a given index. Once again we use the square bracket notation.
60
00:03:56.000 --> 00:04:00.000
So, comma, colors of
61
00:04:00.000 --> 00:04:04.000
index. Save the changes, and now we can see each
62
00:04:04.000 --> 00:04:08.000
element in our colors array. Now starting from
63
00:04:08.000 --> 00:04:12.000
ecma script 6, or ES6, which is the modern
64
00:04:12.000 --> 00:04:16.000
version of JavaScript we have a new kind of loop that is called
65
00:04:16.000 --> 00:04:20.000
for of loop, and that is an ideal way to iterate
66
00:04:20.000 --> 00:04:24.000
over arrays, and that's what you're going to learn in the next lecture.
5660
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.