-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayfindarrayfindindex.js
More file actions
94 lines (65 loc) · 1.66 KB
/
arrayfindarrayfindindex.js
File metadata and controls
94 lines (65 loc) · 1.66 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// JavaScipt array.find() and array.findIndex() methods
// const comments = [
// {
// id: 1,
// comment: 'I love to code.',
// likes: 102,
// user: 'jradness'
// },
// {
// id: 2,
// comment: 'Skate or Die',
// likes: 97,
// user: 'skatemadness'
// },
// {
// id: 3,
// comment: 'Eat. Sleep. Code. Repeat.',
// likes: 106,
// user: 'coder45'
// },
// {
// id: 4,
// comment: 'Skating at Venice Beach rocks',
// user: 'shredz'
// }
// ];
// .find() is a callback that returns a boolean
// loop through the array then when it finds a match returns a true or false and stores a value which is object in the variable
// const id = 3;
// const comment = comments.find((item) => item.id === id);
// console.log(comment);
//comments[4].id
// should get object with id 3 in comments array
// will only try to look for the element until it finds it then stops looking.
***********************************************************************
const comments = [
{
id: 1,
comment: 'I love to code.',
likes: 102,
user: 'jradness'
},
{
id: 2,
comment: 'Skate or Die',
likes: 97,
user: 'skatemadness'
},
{
id: 3,
comment: 'Eat. Sleep. Code. Repeat.',
likes: 106,
user: 'coder45'
},
{
id: 4,
comment: 'Skating at Venice Beach rocks',
user: 'shredz'
}
];
const id = 3;
const comment = comments.find((item) => item.likes === 200);
// when you want to find and index and slice it out of an array
const commentIndex = comments.findIndex((item) => item.id === id);
console.log(commentIndex);