-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind-findIndex.js
78 lines (68 loc) · 2.35 KB
/
find-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
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
/**
* The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
* If no values satisfy the testing function, undefined is returned.
*/
const log = console.log
log()
const array1 = [5, 12, 8, 130, 44]
const found = array1.find((element) => element > 10)
log(found)
const foundAtIndex = array1.findIndex((element) => element > 10)
log(foundAtIndex)
// expected output: 12
log()
// Find an object in an array by one of its properties
const inventory = [
{ name: 'apples', quantity: 2 },
{ name: 'bananas', quantity: 0 },
{ name: 'cherries', quantity: 5 },
]
function isCherries(fruit) {
return fruit.name === 'cherries'
}
log('Find an object in an array by one of its properties',inventory.find(isCherries))
log('Find an object index in an array by one of its properties',inventory.findIndex(isCherries))
// { name: 'cherries', quantity: 5 }
log()
// Using arrow function and destructuring
const result = inventory.find(({ name }) => name === 'cherries')
log('Find an object in an array by one of its properties',result) // { name: 'cherries', quantity: 5 }
log()
// Find a prime number in an array
function isPrime(element, index, array) {
let start = 2
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false
}
}
return element > 1
}
log('Find a prime number in an array',[4, 6, 8, 12].find(isPrime)) // undefined, not found
log('Find a prime number in an array',[4, 5, 8, 12].find(isPrime)) // 5
log()
// Declare array with no elements at indexes 2, 3, and 4
const array = [0,1,,,,5,6];
// Shows all indexes, not just those with assigned values
array.find(function(value, index) {
log('Visited index ', index, ' with value ', value);
});
// Shows all indexes, including deleted
array.find(function(value, index) {
// Delete element 5 on first iteration
if (index === 0) {
log('Deleting array[5] with value ', array[5]);
delete array[5];
}
// Element 5 is still visited even though deleted
log('Visited index ', index, ' with value ', value);
});
// expected output:
// Deleting array[5] with value 5
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value undefined
// Visited index 6 with value 6