-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-array-higher-order-functions.html
118 lines (100 loc) · 3.52 KB
/
js-array-higher-order-functions.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script>
const family = [
{ emoji: "🧒", name: "Edith", age: 8 },
{ emoji: "🙍♀️", name: "Hatty", age: 37 },
{ emoji: "👴", name: "Troy", age: 70 },
{ emoji: "🙍♂️", name: "Raj", age: 35 },
{ emoji: "👵", name: "Rena", age: 68 },
];
console.table(family);
/* functions that don't return anything */
// forEach
// iterates over each item, returns undefined
family.forEach((item) => {
console.log(item);
});
/* functions that return a new array */
// sort
// sorts array in place, array is changed!
const sortedFamily = family.sort((person1, person2) => {
// return negative, positive or 0
return person1.age - person1.age;
});
console.table(family);
console.table(sortedFamily);
// map
// returns a new array, inner function must return something for each iteration
// simple array
const familyNames = family.map((person) => {
return person.name;
});
console.log(familyNames);
// object array
const zombieFamily = family.map((person) => {
if (person.name === "Troy") {
person.emoji = "🧟";
person.age = 10000;
}
return person;
});
console.table(zombieFamily);
// filter
// returns a new array, if nothing matches filter condition it will be an empty array
const filteredFamily = family.filter((person) => {
return person.age < 40;
});
console.table(filteredFamily);
/* functions that return a value */
// find
// returns the first item that matches the condition, returns undefined if nothing found
const found = family.find((person) => {
return person.name === "Troy";
});
console.log(found);
// findIndex
// returns index of first item that matches the condition, return -1 if nothing found
const foundIndex = family.findIndex((person) => {
return person.name === "Troy";
});
// console.log(foundIndex);
// what would you want the index?
family[foundIndex].emoji = "😄";
console.table(family);
// some
// returns true if it finds an item that matches the condition, false if nothing matches
const zombieExists = family.some((person) => {
return person.emoji === "🧟";
});
console.log(zombieExists);
// every
// returns true if all items match the condition, false if any of them don't
console.table(family);
const zombified = zombieFamily.every((person) => {
return person.emoji === "🧟";
});
console.log(zombified);
/* function that return whatever you like ... sort of */
const totalAge = family.reduce((acc, person) => {
return (acc += person.age);
}, 0);
console.log(totalAge);
const namesString = family.reduce((acc, person) => {
return (acc += person.name + ", ");
}, "");
console.log(namesString);
// reduceRight
// same as above but starts at the end and goes backwards
const namesStringRev = family.reduceRight((acc, person) => {
return (acc += person.name + ", ");
}, "");
console.log(namesStringRev);
</script>
</head>
<body></body>
</html>