-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreduce.js
62 lines (52 loc) · 1.28 KB
/
reduce.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
const array1 = [1, 2, 3, 4]
const log = console.log
log()
const reducer = (accumulator, currentValue) => accumulator + currentValue
// 1 + 2 + 3 + 4
log('1 + 2 + 3 + 4 = ', array1.reduce(reducer),0)
// expected output: 10
// 5 + 1 + 2 + 3 + 4
log('5 + 1 + 2 + 3 + 4 = ', array1.reduce(reducer, 5))
// expected output: 15
log()
// Counting instances of values in an object
let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
let countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++
} else {
allNames[name] = 1
}
return allNames
}, {})
log('countedNames', countedNames)
log()
// countedNames is:
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
// Grouping objects by a property
let people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 },
]
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, {})
}
let groupedPeople = groupBy(people, 'age')
log('groupedPeople', groupedPeople)
log()
// groupedPeople is:
// {
// 20: [
// { name: 'Max', age: 20 },
// { name: 'Jane', age: 20 }
// ],
// 21: [{ name: 'Alice', age: 21 }]
// }