-
Notifications
You must be signed in to change notification settings - Fork 0
/
2631-group-by.ts
28 lines (23 loc) · 1007 Bytes
/
2631-group-by.ts
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
// Title: Group By
// Description:
// Write code that enhances all arrays such that you can call the array.groupBy(fn) method on any array and it will return a grouped version of the array.
// A grouped array is an object where each key is the output of fn(arr[i]) and each value is an array containing all items in the original array with that key.
// The provided callback fn will accept an item in the array and return a string key.
// The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.
// Please solve it without lodash's _.groupBy function.
// Link: https://leetcode.com/problems/group-by/
declare global {
interface Array<T> {
groupBy(fn: (item: T) => string): Record<string, T[]>
}
}
Array.prototype.groupBy = function(fn) {
const result = {};
for (const item of this) {
(result[fn(item)] ??= []).push(item);
}
return result;
};
/**
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
*/