1
00:00:00,210 --> 00:00:07,530
There are some interesting ways to get data from objects and arrays using different loop formats, so

2
00:00:07,530 --> 00:00:12,200
there's one called for each, which allows us to loop through array contents.

3
00:00:12,480 --> 00:00:17,320
So it's a method that executes the function once for each array element.

4
00:00:17,340 --> 00:00:19,560
So you see, we've got an example of an array.

5
00:00:19,860 --> 00:00:25,300
There's a for loop that takes the array length and then loops through each one.

6
00:00:25,530 --> 00:00:32,700
There's also the for each loop and then the four L in array and also outputs that information.

7
00:00:32,730 --> 00:00:38,280
So we've got quite a few options looping through the contents of arrays and the same thing for objects

8
00:00:38,520 --> 00:00:44,030
we can loop through an object using the let property in object format.

9
00:00:44,220 --> 00:00:51,040
So the four instatement iterates over all non simbel properties of an object.

10
00:00:51,450 --> 00:00:57,570
So the challenge for this lesson is to output the contents of an array in the console, sautéing each

11
00:00:57,570 --> 00:00:59,760
item and index value.

12
00:01:00,000 --> 00:01:06,540
And then also the second part is output the contents of an object in the console showing each value

13
00:01:06,540 --> 00:01:07,590
and property.

14
00:01:07,800 --> 00:01:12,090
So go ahead, pause the video and I'll show you the solution coming up.

15
00:01:12,120 --> 00:01:14,550
So first of all, we need to create an object.

16
00:01:14,550 --> 00:01:21,270
So let's construct a really simple object of a value of one really, really simple object here.

17
00:01:21,420 --> 00:01:25,260
And this will work for even more complex objects as well, of course.

18
00:01:25,270 --> 00:01:30,000
So we refresh and we see we've got a nice, simple object that we can work with.

19
00:01:30,150 --> 00:01:34,560
So the first thing that we want to do is loop through the contents of this object.

20
00:01:34,890 --> 00:01:39,960
So looping through and we can get the property in object.

21
00:01:39,960 --> 00:01:41,780
So C we get the keys.

22
00:01:41,790 --> 00:01:47,940
So A, B, C, so we know from the object section that there's a number of ways that we can get that

23
00:01:47,940 --> 00:01:49,060
object value.

24
00:01:49,290 --> 00:01:56,730
So using the property, we can get it using the bracket notation and we can get the values as well.

25
00:01:56,730 --> 00:01:58,540
So we get the property and the value.

26
00:01:58,830 --> 00:02:05,220
So if we have an array, so let's also create a really simple array and this is a typical for loop.

27
00:02:05,240 --> 00:02:09,400
So you'll see these as well used in order to get contents out of the array.

28
00:02:09,930 --> 00:02:17,370
So looping through and while W is less than array and we know that arrays have a length value so we

29
00:02:17,370 --> 00:02:22,310
can access that and then incrementing w sort of a way out of our loop.

30
00:02:22,320 --> 00:02:31,680
So log and we can log out the value of W value that's corresponds with the index of W so see we've got

31
00:02:31,680 --> 00:02:36,120
the value as well as the corresponding and I'm going to shorten this one as well.

32
00:02:36,600 --> 00:02:41,520
So do it comma, separate it within the console so it's a little bit cleaner and better to understand.

33
00:02:41,850 --> 00:02:47,580
So this is the second one here where we're looping through the array contents and we're getting the

34
00:02:47,580 --> 00:02:53,820
essentially the index value with W as we loop through and we're getting the values that's corresponding

35
00:02:53,820 --> 00:02:55,080
with the item in the array.

36
00:02:55,500 --> 00:03:00,660
Now the other option series had quite a few different ways that we can get the content out so we can

37
00:03:00,660 --> 00:03:04,800
also go element in array and then loop through that.

38
00:03:04,800 --> 00:03:11,460
And we've got a value for element and we also have a value for the array because the element is going

39
00:03:11,460 --> 00:03:15,480
to be the index value being returned back within the console like that.

40
00:03:15,690 --> 00:03:20,590
So we get the content the same way that we can then of course utilise within our code.

41
00:03:20,970 --> 00:03:29,550
And then lastly, there's also the for each method which will run through each one of the elements within

42
00:03:29,580 --> 00:03:30,100
that array.

43
00:03:30,300 --> 00:03:35,430
We can get back the index value if we want, and then we can also get back that entire array.

44
00:03:35,700 --> 00:03:37,440
So there's three parameters there.

45
00:03:37,560 --> 00:03:41,970
And we can only sometimes if we only want just the first one, we don't need the index value.

46
00:03:42,270 --> 00:03:46,530
If depending on what we're using the code for, we can leave the other ones out.

47
00:03:46,540 --> 00:03:48,060
So those ones are optional.

48
00:03:48,420 --> 00:03:55,560
So outputting into the console, we're going to output the index as well as the element that's corresponds

49
00:03:55,560 --> 00:03:56,550
with that index value.

50
00:03:56,880 --> 00:04:00,270
So C it returns back the same thing as the for loop.

51
00:04:00,450 --> 00:04:02,340
And you can see the contents are the same.

52
00:04:02,610 --> 00:04:08,070
And if you're curious about what's within array, it's actually just going to constantly output that

53
00:04:08,070 --> 00:04:08,800
full array.

54
00:04:09,090 --> 00:04:14,100
So this is something that isn't I don't see very often, but it is an option.

55
00:04:14,100 --> 00:04:19,260
If you do want to make use of that, then you can output the entire contents of the array each time

56
00:04:19,260 --> 00:04:19,890
as well.
