-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind-function.js
65 lines (59 loc) · 1.8 KB
/
find-function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//example - 1
/*find first number which is grater than 4*/
let numbers = [1, 2, 3, 4, 5];
let firstNumberGreaterThanFour = numbers.find(function (currentValue) {
return currentValue > 4;
});
console.log("FIRST_NUMBER_GREATER_THAN_FOUR: ", firstNumberGreaterThanFour);
//example - 2
/*callback function in the find function takes 3 parameter: value,index and the array*/
let arrayTwo = [4, 2, 3, 5, 7];
let firstOddNumber = arrayTwo.find(function (currentValue, currentIndex, arrayTwo) {
console.log("INDEX: ", currentIndex, " VALUE: ", currentValue);
return currentValue % 2 !== 0;
});
console.log("FIRST_ODD_NUMBER: ", firstOddNumber);
//example - 3
let reactApp = {
name: "REACT_APP",
libs: [
{
id: 1,
name: "react"
},
{
id: 2,
name: "react-dom"
},
{
id: 3,
name: "react-router-dom"
},
{
id: 4,
name: "rxjs"
}
],
findById: function (id) {
this.libs.find(function (currentValue, currentIndex) {
let libInfo = null;
if (id === currentValue.id) {
libInfo = currentValue;
console.log("Library ", currentValue.name, " for ", this.name, " found at index ", (currentIndex + 1));
}
return libInfo;
}, this);
},
findByIdUpdated: function (id) {
this.libs.find((currentValue, currentIndex) => {
let libInfo = null;
if (id === currentValue.id) {
libInfo = currentValue;
console.log("Library ", currentValue.name, " for ", this.name, " found at index ", (currentIndex + 1));
}
return libInfo;
});
}
}
reactApp.findById(2);
reactApp.findByIdUpdated(2);