The introduction of Object.groupBy allows for a streamlined, native approach to organizing collections of data based on user defined criterion; thus simplifying the task of data analysis and categorization without the need for managing third-party dependencies.
Using Object.groupBy
Using Object.groupBy
is simple and straight-forward. If you have previously used Lodash groupBy, then you are already familiar with it’s API. Object.groupBy
accepts an array and a callback function which defines the grouping logic, and returns an object of groupings based on the callback function’s returned key
.
For example, we can group employees by department as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | const employees = [ {id: 123, department: 'Engineering', name: 'John Doe', doh: '12/22/20'}, {id: 321, department: 'Product', name: 'Jane Smith', doh: '06/12/19'}, {id: 213, department: 'Product', name: 'Fred Jones', doh: '03/13/12'}, {id: 312, department: 'Engineering', name: 'Kim Tyler', doh: '12/19/21'}, ]; const departments = Object.groupBy(employees,({department}) => department); console.log(departments); // { // Engineering: [ // {id: 123, department: 'Engineering', name: 'John Doe', ...}, // {id: 312, department: 'Engineering', name: 'Kim Tyler', ...} // ], // Product: [ // {id: 321, department: 'Product', name: 'Jane Smith', ...}, // {id: 213, department: 'Product', name: 'Fred Jones', ...} // ] // } |
In the above example, we see that the provided array can easily be grouped into specific properties, in this case, by department. We can just as easily have grouped the array by any other property as well, such as date of hire (doh) to categories by employee tenure (more on this shortly ).
Indeed, Object.groupBy
is particularly useful for grouping collections of objects; however, it is not restricted to objects alone, it can also be used to create grouping primitives as well:
1 2 3 4 5 6 7 8 | const values = [true, false, true, true, false]; const {enabled, disabled} = Object.groupBy( values, value => value ? 'enabled' : 'disabled' ); console.log(enabled.length); // 3 console.log(disabled.length); // 2 |
While the above examples are useful in their own right, the real power of Object.groupBy
is revealed when more complex logic is required for determining groupings. For example, we can group the employees
array by tenure as follows:
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 | const employees = [ {id: 123, department: 'Engineering', name: 'John Doe', doh: '12/22/20'}, {id: 321, department: 'Product', name: 'Jane Smith', doh: '06/12/19'}, {id: 213, department: 'Product', name: 'Fred Jones', doh: '03/13/12'}, {id: 312, department: 'Engineering', name: 'Kim Tyler', doh: '12/19/21'}, ]; const groupByTenure = (employees, target) => { const year = (1000 * 60 * 60 * 24 * 365.25); return Object.groupBy(employees,({doh}) => { const tenure = (target - new Date(doh)) / year; return ( tenure > 1 ? ( tenure <= 2 ? 'development' : 'retention' ) : 'onboarding' ) }) }; console.log( groupByTenure(employees, new Date('11/28/22')) ); // { // "development": [{ // "id": 123, // "department": "Engineering", // "name": "John Doe", // "doh": "12/22/20" // }], // "retention": [{ // "id": 321, // "department": "Product", // "name": "Jane Smith", // "doh": "06/12/19" // }, { // "id": 213, // "department": "Product", // "name": "Fred Jones", // "doh": "03/13/12" // }], // "onboarding": [{ // "id": 312, // "department": "Engineering", // "name": "Kim Tyler", // "doh": "12/19/21" // }] // } |
Concluding Thoughts
New features such as Object.groupBy
serve to highlight the TC39 Committee’s commitment to providing developers with powerful tools which simplify common tasks. By introducing a native facility for grouping objects, Object.groupBy
simplifies overhead and maintainability while also opening up new opportunities for native data aggregation and analysis.
Update:
{Sorry, Comments are currently Closed! }