-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy.js
More file actions
124 lines (96 loc) · 2.96 KB
/
study.js
File metadata and controls
124 lines (96 loc) · 2.96 KB
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
119
120
121
122
123
124
function migratoryBirds(arr) {
let objectArr = arr.reduce((obj, b) => {
obj[b] = ++obj[b] || 1;
return obj;
}, {});
// console.log(objectArr)
const maxVal = Math.max(...Object.values(objectArr));
const num = Object.keys(objectArr).find(key => objectArr[key] === maxVal);
return num;
}
// let arr = [1, 4, 4, 4, 5, 3];
// console.log(migratoryBirds(arr));
// const array = [1, 2, 3, 4];
// const initialValue = 0;
// const sumWithInitial = array.reduce((prev, curr) => {
// // console.log(prev);
// console.log({ prev, curr });
// }, 0);
// console.log(sumWithInitial);
// arr.reduce((prev, curr, index, array) => {
// console.log(curr);
// });
// const getMax = (a, b) => Math.max(a, b);
// let result = [1, 100].reduce(getMax, 50);
// console.log(result);
// console.log([50].reduce(getMax, 50));
// function reducer(prev, curr, index) {
// const returns = prev + curr;
// console.log(
// `previous: ${previous}, current: ${current}, index: ${index}, returns: ${returns}`
// );
// return returns;
// }
// arr.reduce(reducer);
const objects = [{ x: 1 }, { x: 2 }, { x: 3 }];
const sum = objects.reduce((prev, curr) => prev + curr.x, 0);
// console.log(sum);
const flattened = [
[0, 1],
[2, 3],
[4, 5],
];
// console.log(flattened.reduce((prev, curr) => prev.concat(curr), []));
const names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
const countedNames = names.reduce((allNames, name) => {
allNames[name] ??= 0;
allNames[name]++;
return allNames;
}, {});
// let na = undefined;
// console.log(na ?? "Name doesn't exist");
// console.log(countedNames);
// Grouping object by property
const people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 },
];
function groupBy(objectArray, property) {
return objectArray.reduce((acc, obj) => {
const key = obj[property];
acc[key] ??= [];
acc[key].push(obj);
return acc;
}, {});
}
console.log(groupBy(people, 'age'));
const friends = [
{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 },
{ name: 'Bob', books: ['War and peace', 'Romeo and Juliet'], age: 26 },
{ name: 'Alice', books: ['The Lord of the Rings', 'The Shining'], age: 18 },
];
const allbooks = friends.reduce(
(previousValue, currentValue) => [...previousValue, ...currentValue.books],
['Alphabet']
);
const allBooks = friends.reduce(
(prev, curr) => [...prev, ...curr.books],
['Alphabets']
);
console.log(allbooks);
console.log(allBooks);
// Remove duplicate in an array
const myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd'];
const distinctArray = myArray.reduce((prev, curr) => {
if (!prev.includes(curr)) prev.push(curr);
return prev;
}, []);
console.log(distinctArray);
// Replace .filter().map() with .reduce()
const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];
const doublePositiveNumbers = numbers.reduce((prev, curr) => {
if (curr > 0) prev.push(curr * 2);
return prev;
}, []);
console.log(doublePositiveNumbers);