1
00:00:02,380 --> 00:00:08,760
So how could we fix this here and find an object in an array index of doesn't work?

2
00:00:08,830 --> 00:00:16,140
Well you have another method or two other methods which you can use in such a situation. So let's say

3
00:00:16,290 --> 00:00:18,600
Manuel is the person I want to find,

4
00:00:18,600 --> 00:00:22,560
well then you can use person data and then there is a find method,

5
00:00:22,560 --> 00:00:25,950
that's also a method which is available on every real array

6
00:00:25,950 --> 00:00:27,930
and now find is an interesting method.

7
00:00:27,960 --> 00:00:34,110
It takes an argument but the argument it takes is actually another function.

8
00:00:34,110 --> 00:00:39,900
Now this is a typical use case for an anonymous function, though of course you could also define a function

9
00:00:39,900 --> 00:00:42,810
somewhere and then just pass in a reference to that function,

10
00:00:42,840 --> 00:00:48,090
so the function name but since this often is a function which you only need to run in this place and

11
00:00:48,090 --> 00:00:53,710
which you never call from anywhere else in your code, this is a perfect scenario for an anonymous function.

12
00:00:53,710 --> 00:00:56,610
Now of course it's not just a function like this which does nothing,

13
00:00:56,670 --> 00:01:01,390
instead it's a function that can accept up to three arguments.

14
00:01:01,530 --> 00:01:06,860
The first argument will always be a single object of that array,

15
00:01:06,900 --> 00:01:08,590
so we could name this person,

16
00:01:08,610 --> 00:01:10,570
you can choose any name you want here.

17
00:01:10,590 --> 00:01:15,140
The second argument will always be the index of that single element,

18
00:01:15,210 --> 00:01:18,920
so you could name it index or idx or whatever you want.

19
00:01:19,020 --> 00:01:22,620
The third argument will be the full array,

20
00:01:22,620 --> 00:01:29,190
so persons for example. Now who is calling that function? Javascript that will call it for you

21
00:01:29,220 --> 00:01:35,910
so to say, find executes on this array and what it does is it executes this anonymous function which you

22
00:01:35,910 --> 00:01:36,710
pass to it

23
00:01:36,720 --> 00:01:42,120
or this function in general what you pass to it on every element in person data.

24
00:01:42,120 --> 00:01:49,050
So it will execute this function here on every element in person data and will then for every execution

25
00:01:49,050 --> 00:01:49,870
pass in

26
00:01:49,890 --> 00:01:55,820
that element it's currently looking at, then the index of that element and then the full array.

27
00:01:55,860 --> 00:02:01,110
Now of course, you can also use find on any array, even if it's an array full of numbers or text, it's

28
00:02:01,110 --> 00:02:07,320
not restricted to arrays full of objects, that's just a use case where it's particularly helpful because

29
00:02:07,320 --> 00:02:12,000
it can help us identify an object or a value if it's a reference value

30
00:02:12,020 --> 00:02:18,680
because for primitive values, often index of helps us get what we want, though that of course returns the

31
00:02:18,680 --> 00:02:19,250
index

32
00:02:19,280 --> 00:02:23,000
but then we could use that index to easily access the value behind that index,

33
00:02:23,000 --> 00:02:23,720
right?

34
00:02:23,870 --> 00:02:24,770
But back to find,

35
00:02:24,770 --> 00:02:31,430
so we execute this on every item in person data and it then expects that this function which you pass

36
00:02:31,430 --> 00:02:35,990
to find returns true for the element you're looking for,

37
00:02:36,140 --> 00:02:40,680
so for the element you are trying to find and false for all other elements.

38
00:02:40,700 --> 00:02:44,090
So in here, we have to return something and that should be a boolean,

39
00:02:44,090 --> 00:02:46,870
so typically in here you have some kind of comparison.

40
00:02:47,060 --> 00:02:53,690
So we could compare person.name because we know we execute this on an array of person data,

41
00:02:53,720 --> 00:02:57,410
so we have a bunch of objects in there which have a name property

42
00:02:57,410 --> 00:02:59,660
and this is a single element of the array,

43
00:02:59,660 --> 00:03:00,530
don't forget that

44
00:03:00,620 --> 00:03:08,240
so it will be an object with a name property and we can check whether that is equal to Manuel for example

45
00:03:08,240 --> 00:03:09,370
in this case

46
00:03:09,620 --> 00:03:14,780
and this should find us that one object where the name is Manuel.

47
00:03:14,780 --> 00:03:20,480
Now if you have multiple objects with a name of Manuel, it will still stop after the first hit, just like

48
00:03:20,480 --> 00:03:27,350
index of, it will stop after that and not return all fitting elements but only the first one. So find

49
00:03:27,350 --> 00:03:33,380
returns us Manuel, here therefore I'm not even using that index or that entire array and quite often,

50
00:03:33,380 --> 00:03:38,020
you'll not need that but if you need it for some reason, in that function, you have it available and therefore

51
00:03:38,020 --> 00:03:44,960
now if I console log Manuel here, we hopefully console log a Javascript object which is having a

52
00:03:44,960 --> 00:03:46,490
name of Manuel.

53
00:03:46,580 --> 00:03:49,970
So if we now reload, that's looking good.

54
00:03:49,970 --> 00:03:55,060
Now one important note about find, it returns the same object as you have in the array here,

55
00:03:55,070 --> 00:03:57,660
it does not copy it or anything like that.

56
00:03:57,710 --> 00:04:03,860
So if I then would manipulate Manuel's name here and set it to Anna

57
00:04:03,890 --> 00:04:11,720
which is pretty drastic change I guess but let's still do it, then I will not only see that here when

58
00:04:11,720 --> 00:04:18,950
I log Manuel but also if I log the entire person data array again. If I do that though and I reload,

59
00:04:18,950 --> 00:04:25,010
you see here we have Anna but we also have that here in the array because we're working with the same

60
00:04:25,190 --> 00:04:29,090
reference value, with the exact same object, that's important to understand,

61
00:04:29,150 --> 00:04:32,160
find does not create a copy.

62
00:04:32,170 --> 00:04:41,780
Now just like find, you also have find index, so you can get Anna or max index here by using person data

63
00:04:42,050 --> 00:04:46,070
find index and then it takes the exact same function,

64
00:04:46,160 --> 00:04:48,050
so exactly in the same way,

65
00:04:48,050 --> 00:04:54,110
now I just want to look for Max here and the difference is that it will not return the matching item

66
00:04:54,220 --> 00:04:57,380
of the array but the index of that item.

67
00:04:57,380 --> 00:05:05,900
So if I now console log max index down there, we should see 0 because that max object is the first element

68
00:05:05,930 --> 00:05:08,910
of that array and therefore has an index of 0.

69
00:05:08,920 --> 00:05:13,970
So if we save that, reload, indeed here we see 0 being printed

70
00:05:14,000 --> 00:05:15,420
so that also works.

71
00:05:15,470 --> 00:05:17,690
So let's find and find index.
