1
00:00:02,070 --> 00:00:04,910
Now we also have a for in loop.

2
00:00:04,910 --> 00:00:06,820
And the difference is that for in

3
00:00:06,820 --> 00:00:09,283
is meant to be used on objects.

4
00:00:10,190 --> 00:00:15,190
Let's say we have a loggedInUser object here,

5
00:00:15,590 --> 00:00:18,900
and I'm just coming up with some example data here.

6
00:00:18,900 --> 00:00:19,900
We're still of course

7
00:00:19,900 --> 00:00:23,280
just exploring some dummy use cases here.

8
00:00:23,280 --> 00:00:25,130
And that loggedInUser has a name,

9
00:00:25,130 --> 00:00:26,210
which is Max,

10
00:00:26,210 --> 00:00:28,230
age which is 32.

11
00:00:28,230 --> 00:00:31,330
And an isAdmin property,

12
00:00:31,330 --> 00:00:33,530
which is true.

13
00:00:33,530 --> 00:00:35,520
And here we see a boolean value

14
00:00:35,520 --> 00:00:38,040
created and assigned just like this,

15
00:00:38,040 --> 00:00:42,340
because that is also a common use case for booleans

16
00:00:42,340 --> 00:00:44,140
that we store them just like this

17
00:00:44,140 --> 00:00:49,120
in variables or properties that act as flex.

18
00:00:49,120 --> 00:00:52,280
So that tells us whether a user is an admin or not,

19
00:00:52,280 --> 00:00:53,313
for example.

20
00:00:54,510 --> 00:00:57,290
And now that I got these free properties,

21
00:00:57,290 --> 00:01:01,280
we could output these key value pairs here

22
00:01:01,280 --> 00:01:03,900
with help of a for in loop.

23
00:01:03,900 --> 00:01:06,330
If we had some code that needs to access

24
00:01:06,330 --> 00:01:07,760
all these properties

25
00:01:07,760 --> 00:01:10,790
and transform the data into some other format,

26
00:01:10,790 --> 00:01:12,663
or do anything else with them.

27
00:01:13,770 --> 00:01:14,603
For this again,

28
00:01:14,603 --> 00:01:17,220
we started with the for keyword,

29
00:01:17,220 --> 00:01:19,960
and then again we try to help with constant,

30
00:01:19,960 --> 00:01:22,400
but this constant will then actually hold

31
00:01:22,400 --> 00:01:26,030
the different property names,

32
00:01:26,030 --> 00:01:29,060
or keys as we can also call them.

33
00:01:29,060 --> 00:01:31,950
And hence you very often named this key,

34
00:01:31,950 --> 00:01:33,370
But the name is up to you.

35
00:01:33,370 --> 00:01:35,647
We could also name it propertyName.

36
00:01:37,470 --> 00:01:38,820
The important thing to note,

37
00:01:38,820 --> 00:01:40,750
it's just that it will be the name,

38
00:01:40,750 --> 00:01:43,250
not the value and not both together.

39
00:01:43,250 --> 00:01:44,533
Just the name.

40
00:01:45,990 --> 00:01:48,400
Then we use the in keyword here,

41
00:01:48,400 --> 00:01:50,610
and then we point at the object

42
00:01:50,610 --> 00:01:52,190
through which we want to loop.

43
00:01:52,190 --> 00:01:56,527
So in this case for propertyName in loggedInUser,

44
00:01:58,260 --> 00:02:02,030
Now in here we can now console log propertyName

45
00:02:02,030 --> 00:02:04,550
to see what's stored in there,

46
00:02:04,550 --> 00:02:06,590
and if we save that code,

47
00:02:06,590 --> 00:02:10,000
you will see name, age and isAdmin

48
00:02:10,000 --> 00:02:12,730
being printed out here at the bottom.

49
00:02:12,730 --> 00:02:15,190
Because these are the three property names

50
00:02:15,190 --> 00:02:16,233
we have in here.

51
00:02:17,340 --> 00:02:20,920
Now, if you want to get access to the values as well,

52
00:02:20,920 --> 00:02:25,050
that's possible with help all of a special kind of syntax

53
00:02:25,050 --> 00:02:26,733
which we haven't seen yet.

54
00:02:27,660 --> 00:02:31,030
We can console log the loggedInUser,

55
00:02:31,030 --> 00:02:34,470
and now dynamically access the different properties

56
00:02:34,470 --> 00:02:36,023
stored in the user.

57
00:02:36,870 --> 00:02:38,010
Up to this point,

58
00:02:38,010 --> 00:02:40,710
when we wanted to access a property,

59
00:02:40,710 --> 00:02:43,890
we always did that with the dot notation.

60
00:02:43,890 --> 00:02:45,870
To access loggedInUser.name,

61
00:02:45,870 --> 00:02:46,933
for example.

62
00:02:47,820 --> 00:02:49,530
But that won't work here

63
00:02:49,530 --> 00:02:51,720
since the property I want to access

64
00:02:51,720 --> 00:02:55,480
is actually different in every execution.

65
00:02:55,480 --> 00:02:58,830
And to access such property names dynamically,

66
00:02:58,830 --> 00:03:00,480
we can use a syntax

67
00:03:00,480 --> 00:03:03,670
which we already know from erase.

68
00:03:03,670 --> 00:03:08,400
We can't add square brackets here after loggedInUser,

69
00:03:08,400 --> 00:03:11,730
even though that's not an array but an object,

70
00:03:11,730 --> 00:03:15,260
but there you can use square brackets as well.

71
00:03:15,260 --> 00:03:17,560
And between those square brackets,

72
00:03:17,560 --> 00:03:20,390
you can put the property name as a string.

73
00:03:20,390 --> 00:03:21,230
So for example,

74
00:03:21,230 --> 00:03:24,980
name, I could access this like that as well.

75
00:03:24,980 --> 00:03:28,773
And that would be equivalent to loggedInUser.name.

76
00:03:30,330 --> 00:03:32,900
But since we have this syntax,

77
00:03:32,900 --> 00:03:35,930
we can now also not hard-code the propertyName

78
00:03:35,930 --> 00:03:37,090
as a string here,

79
00:03:37,090 --> 00:03:40,350
but instead refer to propertyName.

80
00:03:40,350 --> 00:03:42,960
our helper constant here.

81
00:03:42,960 --> 00:03:44,950
And property name indeed holds the

82
00:03:44,950 --> 00:03:47,900
different property names as strings.

83
00:03:47,900 --> 00:03:51,170
So therefore this line of code will dynamically access the

84
00:03:51,170 --> 00:03:54,790
values for those different propertyNames,

85
00:03:54,790 --> 00:03:57,263
which are different for every execution.

86
00:03:58,490 --> 00:04:02,483
That's how we can access propertyNames dynamically.

87
00:04:03,480 --> 00:04:06,110
This does not work with the dot notation

88
00:04:06,110 --> 00:04:08,767
because of you had loggedInUser.propertyName,

89
00:04:09,650 --> 00:04:12,980
it would look for a property named propertyName,

90
00:04:12,980 --> 00:04:15,530
not having the name stored as a value

91
00:04:15,530 --> 00:04:18,000
inside of propertyName,

92
00:04:18,000 --> 00:04:19,470
so that won't work.

93
00:04:19,470 --> 00:04:21,310
But with the square bracket notation,

94
00:04:21,310 --> 00:04:23,990
we can dynamically access the values

95
00:04:23,990 --> 00:04:27,290
by that dynamic propertyName.

96
00:04:27,290 --> 00:04:29,200
And hence, if we save that,

97
00:04:29,200 --> 00:04:31,640
we get name, Max,

98
00:04:31,640 --> 00:04:33,000
age, 32

99
00:04:33,000 --> 00:04:34,910
and is Admin, true.

100
00:04:34,910 --> 00:04:37,193
So that's then working as it should.

