-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup By.js
More file actions
51 lines (40 loc) · 1.59 KB
/
Group By.js
File metadata and controls
51 lines (40 loc) · 1.59 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
// 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 which generate 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.
// Example 1:
// Input:
// array = [
// {"id":"1"},
// {"id":"1"},
// {"id":"2"}
// ],
// fn = function (item) {
// return item.id;
// }
// Output:
// {
// "1": [{"id": "1"}, {"id": "1"}],
// "2": [{"id": "2"}]
// }
// Explanation:
// Output is from array.groupBy(fn).
// The selector function gets the "id" out of each item in the array.
// There are two objects with an "id" of 1. Both of those objects are put in the first array.
// There is one object with an "id" of 2. That object is put in the second array.
// Code
/**
* @param {Function} fn
* @return {Object}
*/
Array.prototype.groupBy = function(fn) {
return this.reduce((grouped, item) => {
const key = fn(item); // Compute the key using the provided function
if (!grouped[key]) {
grouped[key] = []; // Initialize an array for the key if it doesn't exist
}
grouped[key].push(item); // Add the current item to the corresponding group
return grouped;
}, {}); // Start with an empty object
};