-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterators-findIndex.js
46 lines (31 loc) · 1.22 KB
/
iterators-findIndex.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
// Iterators .findIndex() Method
// returns index of first el that evals to true in the callback func
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];
// find the index of el with value: 'elephant'
// save returned value to variable named foundAnimal
const foundAnimal = animals.findIndex(animal => {
return animal === 'elephant';
});
// find the index of the first animal that starts with 's'
const startsWithS = animals.findIndex(animal => {
return animal[0] === 's';
});
console.log(foundAnimal, startsWithS); // 7 3
console.log(animals[7]); // elephant
console.log(animals[3]); // seal
// Define a callback function before using it in an iterator
const cats = ['calico', 'siamese', 'tabby'];
function startsWithT (cat) {
return cat[0] === 't';
}
const indexT = cats.findIndex(startsWithT);
console.log(cats[indexT]); // tabby
function endsWithE (cat) {
return cat[cat.length -1] === 'e';
}
const indexE = cats.findIndex(endsWithE);
console.log(cats[indexE]); // siamese
// Chain two iterator methods on same array
const iteratorChain = cats.filter(endsWithE).filter(cat => cat[0] === "s")
console.log('iteratorChain: ', iteratorChain);
console.log(iteratorChain.length)