1
00:00:02,300 --> 00:00:04,250
So that were symbols,

2
00:00:04,250 --> 00:00:11,570
now let's move on to iterations and generators and let's start with iterators. An iterator is simply

3
00:00:11,660 --> 00:00:20,250
an object in the end which has a next method, which then in turn returns a result of a certain structure.

4
00:00:20,360 --> 00:00:28,940
For that let's say we have another constant, company and that holds an object where we have employees

5
00:00:29,150 --> 00:00:33,740
and there I got Max and I got Manu and I got Anna in there.

6
00:00:33,740 --> 00:00:36,440
Now this is a regular object,

7
00:00:36,440 --> 00:00:44,600
we make it iterable or we turn it into an iterator or you could say by adding a next method here

8
00:00:44,690 --> 00:00:46,700
which we can add like this,

9
00:00:46,700 --> 00:00:56,050
now the name has to be exactly next. Now in this next method here, we then should return an object where

10
00:00:56,050 --> 00:01:01,250
we have a value property, this could be employees

11
00:01:01,280 --> 00:01:04,190
this, employees zero

12
00:01:04,190 --> 00:01:10,670
so the first element in the employees array and a done property which simply signals with a boolean

13
00:01:10,990 --> 00:01:13,250
whether we got more values we can output or not.

14
00:01:14,180 --> 00:01:22,310
Well then we should probably also have another property here, current employee which initially is zero which

15
00:01:22,310 --> 00:01:25,920
we use to track which employee we already output

16
00:01:25,940 --> 00:01:34,640
so that here we can access this current employee and store that in a returned value variable or constant, I'm

17
00:01:34,640 --> 00:01:35,760
not going to change this

18
00:01:35,900 --> 00:01:42,920
and thereafter we can set this current employee to current employee plus one by incrementing it and then

19
00:01:42,920 --> 00:01:47,260
we return our value here. To be precise,

20
00:01:47,310 --> 00:01:53,730
we also want to check if we still have employees left through which we can loop by checking if this current

21
00:01:53,730 --> 00:01:58,990
employee is greater or equal than this employees length.

22
00:02:00,690 --> 00:02:05,430
We know if this is the case, then we're out of employees because we then have no more employees left,

23
00:02:06,090 --> 00:02:13,020
so here we could return a value of this current employee to return the number for which we failed and

24
00:02:13,020 --> 00:02:17,920
set done to true to signal that we are done looping through that.

25
00:02:18,010 --> 00:02:21,570
Now what's the idea behind this next method though?

26
00:02:21,570 --> 00:02:29,410
Well now we can console log company next and execute this a couple of times down there.

27
00:02:29,520 --> 00:02:32,240
If we save that and we reload,

28
00:02:32,240 --> 00:02:36,850
you see we print all different employees until we're done.

29
00:02:36,900 --> 00:02:42,780
So what we do here in the end is we create our own method that allows us to loop through a specific

30
00:02:42,780 --> 00:02:48,060
field of this object and we could have any looping logic we want in here.

31
00:02:48,060 --> 00:02:52,650
So this in the end kind of makes this a loopable object,

32
00:02:52,650 --> 00:02:58,650
right now we can't use it with for/of but we soon will be able to do so but we have an object where

33
00:02:58,650 --> 00:03:05,580
we have a method which we can call multiple times to go through any logic of our choice, in this case

34
00:03:05,580 --> 00:03:11,970
to go through all employees but of course it's totally up to you through which field or whatever you want

35
00:03:12,180 --> 00:03:13,890
to loop through here.

36
00:03:13,890 --> 00:03:19,260
So we have a method which we can call multiple times and every time it will return something different

37
00:03:19,590 --> 00:03:26,310
and not just that, it will actually go through some list of data, it will exhaust some list of data and

38
00:03:26,310 --> 00:03:32,520
when we exhausted that list, we'll not get an error, we'll simply get a response, we'll get back a value

39
00:03:32,700 --> 00:03:39,150
where we see that we're done because of this uniform return value where we always have a done property

40
00:03:39,210 --> 00:03:40,530
which is true or false,

41
00:03:40,560 --> 00:03:46,450
we also know if we can call next again and have more values left or if that's not the case.

42
00:03:46,740 --> 00:03:52,770
Now this alone as I said might not be totally helpful yet but this already is a logic you could use

43
00:03:52,770 --> 00:03:59,280
in some applications where you have an object which you use as a data storage and you want to go through

44
00:03:59,280 --> 00:04:04,110
some custom logic there to output some data which is stored in there,

45
00:04:04,110 --> 00:04:10,110
you don't want to use a for/in loop because you're maybe not interested in all the properties of this

46
00:04:10,110 --> 00:04:15,600
object but just in some of the values in one specific property of this object and then such a custom

47
00:04:15,600 --> 00:04:22,030
looping logic could be quite interesting here because what you could of course do with that is you could

48
00:04:22,030 --> 00:04:29,090
create a while loop and have a result here or let's say an employee

49
00:04:29,740 --> 00:04:40,690
and for that you call company next, then you have a while loop and as long as employee is not done, you

50
00:04:40,690 --> 00:04:46,630
continue looping and then you can console log employee value here or do whatever you want to do with

51
00:04:46,630 --> 00:04:51,180
it in the while loop and thereafter, set employee equal to company

52
00:04:51,220 --> 00:04:52,280
next again

53
00:04:52,420 --> 00:04:57,520
and now we have a dynamic loop, not a for/of loop but still a loop which will in the end go through

54
00:04:57,550 --> 00:05:00,170
all employees with our own iterator logic.

55
00:05:00,220 --> 00:05:06,280
So if we save that and we reload, it outputs the three employees we have and then it stops because then

56
00:05:06,490 --> 00:05:13,540
done is true and therefore this exit condition kicks in or this running condition is no longer true

57
00:05:13,750 --> 00:05:15,670
and therefore we make it out of that loop.

58
00:05:15,730 --> 00:05:21,700
So this would be one example how you can use your own iterator, company, which has this next method which

59
00:05:21,700 --> 00:05:27,640
then in turn allows you to have your own looping logic for whichever field or whichever logic related

60
00:05:27,640 --> 00:05:30,070
to this company object you might have.
