Skip to content

Commit 8bde8ac

Browse files
day 23 task complete
1 parent 8d02c66 commit 8bde8ac

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

day23/leetcode2631.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 2631. Group By
2+
// URL -> https://leetcode.com/problems/group-by
3+
4+
/**
5+
* @param {Function} fn
6+
* @return {Object}
7+
*/
8+
Array.prototype.groupBy = function (fn) {
9+
let obj = {};
10+
11+
for (const item of this) {
12+
if (obj[fn(item)]) {
13+
obj[fn(item)].push(item);
14+
} else {
15+
obj[fn(item)] = [item];
16+
}
17+
}
18+
19+
return obj
20+
};
21+
22+
/**
23+
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
24+
*/

0 commit comments

Comments
 (0)