forked from nhattruongniit/learn-javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.js
92 lines (76 loc) · 2.43 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
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
// array reduce: https://medium.freecodecamp.org/reduce-f47a7da511a9
// var numbers = [1, 2, 3, 4];
// var result = numbers.reduce(function(a, b) {
// console.log(a, b);
// return a + b;
// })
// console.log(result);
// var orders = [
// { name: 'A', quanlity: 2, unitPrice: 100 },
// { name: 'B', quanlity: 1, unitPrice: 400 },
// { name: 'C', quanlity: 4, unitPrice: 15 }
// ]
// var totalOrder = orders.reduce(function(currentTotal, order) {
// return currentTotal + order.quanlity * order.unitPrice
// }, 0)
// console.log(totalOrder);
// var items = ['Tom', 'Bill', 'Kim'];
// var result1 = items.map(function(item) {
// return '<' + item + '>';
// }).reduce(function(current, text) {
// return current + text
// })
// console.log(result1);
const arraySum = [1, 2, 3, 4, 5];
const sumTotal = arraySum.reduce((accum, total) => accum + total);
console.log('======tinh tong======', sumTotal);
const euros = [29.76, 41.85, 46.5];
const average = euros.reduce((total, amount, index, array) => {
total += amount;
if(index == euros.length - 1) {
return total / array.length
} else {
return total;
}
})
console.log('reduce average: ', average);
const above30 = euros.reduce((total, amount) => {
if(amount > 30) {
total.push(amount);
}
return total;
}, [])
console.log('reduce above > 30: ', above30)
//Creating a Tally with the Reduce Method In JavaScript
const fruitBasket = ['banana', 'cherry', 'orange', 'apple', 'cherry', 'orange', 'apple', 'banana', 'fig'];
const countFruit = fruitBasket.reduce((acc, curr) => {
if(!acc[curr]) {
acc[curr] = 1;
} else {
acc[curr] += 1;
}
// tally[fruit] = (tally[fruit] || 0) + 1;
return acc;
}, {})
console.log('find count fruit duplicate: ', countFruit)
//Flattening an array of arrays with the Reduce Method In JavaScript
const flattenArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const flatt = flattenArray.reduce((total, amount) => {
return total.concat(amount);
}, [])
console.log('Flattening an array: ', flatt);
// flatten color complicated
const colorArray = [
{ a: 'Happy', b: 'Robin', c: ['blue', 'green'] },
{ a: 'Tired', b: 'panther', c: ['blue', 'green', 'red', 'orange'] },
{ a: 'Sad', b: 'Fish', c: ['green', 'red'] },
]
const colors = colorArray.reduce((total, amount) => {
amount.c.forEach(color => {
if(total.indexOf(color) === -1) {
total.push(color);
}
});
return total
}, []);
console.log('Flattening color: ', colors);