1
00:00:00,360 --> 00:00:05,370
You might get a question check that user with such name exists in the real object.

2
00:00:05,610 --> 00:00:08,100
Obviously, it can be not name or not user.

3
00:00:08,250 --> 00:00:13,170
But the idea is that you need to check a property of the object inside an array.

4
00:00:16,480 --> 00:00:19,000
And actually, there are different ways to implement this.

5
00:00:19,180 --> 00:00:25,180
And the worst way possible is by using for Loop, because as I said previously, full loop is super

6
00:00:25,180 --> 00:00:28,990
low level and it is not as a tool for this specific task.

7
00:00:29,170 --> 00:00:31,490
So let's look how it looks like this for a loop.

8
00:00:31,510 --> 00:00:35,530
So here we want to create the function, for example, is name exists.

9
00:00:35,860 --> 00:00:37,780
And here we must pass two arguments.

10
00:00:37,780 --> 00:00:38,980
First of all, it's a name.

11
00:00:39,160 --> 00:00:45,490
And secondly, it is a ray of our users know inside of a must create a property which will be Boolean,

12
00:00:45,610 --> 00:00:48,970
and it will show us that we have this user inside an array.

13
00:00:49,210 --> 00:00:53,920
So here we can make a let exist, and by default, it will be false.

14
00:00:54,250 --> 00:00:55,960
Now we must try to follow up.

15
00:00:55,960 --> 00:01:01,510
So here we have our left eye from zero and I less than user's length.

16
00:01:01,720 --> 00:01:04,000
And here we make an A-plus plus.

17
00:01:04,300 --> 00:01:08,860
Now what we want to do inside for every single element, we must check its name.

18
00:01:09,130 --> 00:01:14,110
This is why here we are checking the user's I don't name equals our name.

19
00:01:14,350 --> 00:01:19,960
And if it equals, then we want to assign inside to exist through after a for loop.

20
00:01:19,960 --> 00:01:22,270
We want to return our exist property.

21
00:01:22,630 --> 00:01:24,580
So yes, of this code will work.

22
00:01:24,700 --> 00:01:25,600
Let's check this out.

23
00:01:25,870 --> 00:01:27,460
But it is super low level.

24
00:01:27,640 --> 00:01:30,160
As you can see, we have, for example, named John.

25
00:01:30,340 --> 00:01:36,100
Let's right here, John and here comma our users, as you can see, widget and true because we have

26
00:01:36,100 --> 00:01:41,680
such user with such name insiders, Ray, if I am right in here, just full, where get and false.

27
00:01:41,920 --> 00:01:46,150
But the problem with this code, it won't show that you're an advanced developer.

28
00:01:46,450 --> 00:01:51,010
This is why I want to show you three other possibilities to ride this code.

29
00:01:51,160 --> 00:01:54,210
And actually, all these three possibilities are OK.

30
00:01:54,550 --> 00:01:59,560
My favorite will be a single line here so we can create a function is name exists.

31
00:01:59,800 --> 00:02:02,050
And here we get a name and the right.

32
00:02:02,290 --> 00:02:08,290
And as you can see here, I didn't write user's battery because now we can use this function on any

33
00:02:08,300 --> 00:02:10,280
data and know what they want to call.

34
00:02:10,300 --> 00:02:12,220
Here is a red dot sum.

35
00:02:12,400 --> 00:02:18,100
And if you don't know what Sam is doing, you can call some on the ray and given side some predicates

36
00:02:18,100 --> 00:02:20,200
so you want to check something inside.

37
00:02:20,500 --> 00:02:27,190
In our case, Sam will return as true if we have at least one single element which fulfills our condition.

38
00:02:27,490 --> 00:02:34,180
So inside Sam, we can write element and we want to check here element name equals our names that were

39
00:02:34,180 --> 00:02:34,960
passed inside.

40
00:02:35,290 --> 00:02:39,670
And this is literally the same code lake result here, but much smaller.

41
00:02:39,700 --> 00:02:45,520
Let's check the sound I religion page and I'm just type in his name exist as you can see where getting

42
00:02:45,530 --> 00:02:45,910
through.

43
00:02:46,120 --> 00:02:52,540
So actually, Sam is the best solution to this question, but it is also possible to use other solutions.

44
00:02:52,810 --> 00:02:54,940
So let's command the sound and try again.

45
00:02:55,210 --> 00:02:58,300
We have here again two arguments name and array.

46
00:02:58,510 --> 00:03:02,200
Now inside we can try and find element with such name.

47
00:03:02,440 --> 00:03:06,340
This is why here let's create an element and we're using here a red dot.

48
00:03:06,340 --> 00:03:08,530
Find we're getting our element.

49
00:03:08,650 --> 00:03:13,180
And here we want to check element dog name equals zone name from the top.

50
00:03:13,570 --> 00:03:16,840
But in this case, our element is actually an object.

51
00:03:16,990 --> 00:03:18,220
It's not a Boolean.

52
00:03:18,430 --> 00:03:24,070
This is where here must wrap our element in Boolean to return Boolean and not an object.

53
00:03:24,310 --> 00:03:26,260
In this case, it will work correctly.

54
00:03:26,560 --> 00:03:27,820
I'm reloading the page.

55
00:03:27,820 --> 00:03:30,220
As you can see, it is working exactly like this.

56
00:03:30,520 --> 00:03:36,340
So the main difference is that we must try it a little bit more code because we must convert it to Boolean.

57
00:03:36,670 --> 00:03:41,080
And the last thing that we can use here is actually not find but find the index.

58
00:03:41,320 --> 00:03:47,080
And if you don't know what, find the index is doing is exactly the same logic, but were found in the

59
00:03:47,080 --> 00:03:48,700
index of this element.

60
00:03:48,940 --> 00:03:52,960
So if we can find the object with search name inside our array.

61
00:03:53,140 --> 00:03:56,350
So here we're not getting our element button index.

62
00:03:56,650 --> 00:04:02,260
And after this, we know that if this index is less than zero, then this item was not found.

63
00:04:02,470 --> 00:04:06,040
So here we can try to return index bigger than zero.

64
00:04:06,220 --> 00:04:08,800
In this case, even though, OK, it is bigger than zero.

65
00:04:08,920 --> 00:04:14,410
We have this element inside the rape, and as you can see, this code is working exactly the same.

66
00:04:14,830 --> 00:04:16,750
So my approach would be SAM.

67
00:04:16,930 --> 00:04:23,200
The next approach will be find index find is also OK, but you should not use for loop because it is

68
00:04:23,200 --> 00:04:25,090
highly inefficient for such a task.
