Goal: to implement a universal function for data grouping, which is often used in data processing and analytics.
The main idea is to iterate over the array and place elements into an object, using the key’s value for grouping.
result = {}).item) in the source array using a loop like forEach.key argument (groupKey = item[key]).groupKey exists in result (if (!result[groupKey])).result[groupKey] = [].item to the array corresponding to that key: result[groupKey].push(item).result.This approach can be elegantly implemented using the Array.prototype.reduce method.
const groupBy = (arr, key) => {
// Use reduce to transform the array into an object.
// accumulator (acc) is the object we will build.
// current (obj) is the current element of the array.
return arr.reduce((acc, obj) => {
// Get the key value for the current object.
const groupKey = obj[key];
// If the accumulator doesn't have an array for this key yet, create one.
if (!acc[groupKey]) {
acc[groupKey] = [];
}
// Add the current object to the array corresponding to its key.
acc[groupKey].push(obj);
// Return the updated accumulator for the next iteration.
return acc;
}, {}); // The initial value for the accumulator is an empty object.
};Solution Analysis:
You need to implement a function groupBy that takes an array of objects arr and a string key. The function should group all objects from the array by the value of their key property and return an object where the keys are the property values, and the values are arrays of objects with that key.
This method is incredibly useful for organizing data, for example, for grouping a list of users by city or products by category.
const data = [
{ city: 'Moscow', temp: 15 },
{ city: 'London', temp: 12 },
{ city: 'Moscow', temp: 18 },
{ city: 'Paris', temp: 20 },
{ city: 'London', temp: 14 }
];
groupBy(data, 'city');
/*
{
"Moscow": [
{ "city": "Moscow", "temp": 15 },
{ "city": "Moscow", "temp": 18 }
],
"London": [
{ "city": "London", "temp": 12 },
{ "city": "London", "temp": 14 }
],
"Paris": [
{ "city": "Paris", "temp": 20 }
]
}
*/groupBy.undefined.Goal: to implement a universal function for data grouping, which is often used in data processing and analytics.
The main idea is to iterate over the array and place elements into an object, using the key’s value for grouping.
result = {}).item) in the source array using a loop like forEach.key argument (groupKey = item[key]).groupKey exists in result (if (!result[groupKey])).result[groupKey] = [].item to the array corresponding to that key: result[groupKey].push(item).result.This approach can be elegantly implemented using the Array.prototype.reduce method.
const groupBy = (arr, key) => {
// Use reduce to transform the array into an object.
// accumulator (acc) is the object we will build.
// current (obj) is the current element of the array.
return arr.reduce((acc, obj) => {
// Get the key value for the current object.
const groupKey = obj[key];
// If the accumulator doesn't have an array for this key yet, create one.
if (!acc[groupKey]) {
acc[groupKey] = [];
}
// Add the current object to the array corresponding to its key.
acc[groupKey].push(obj);
// Return the updated accumulator for the next iteration.
return acc;
}, {}); // The initial value for the accumulator is an empty object.
};Solution Analysis:
You need to implement a function groupBy that takes an array of objects arr and a string key. The function should group all objects from the array by the value of their key property and return an object where the keys are the property values, and the values are arrays of objects with that key.
This method is incredibly useful for organizing data, for example, for grouping a list of users by city or products by category.
const data = [
{ city: 'Moscow', temp: 15 },
{ city: 'London', temp: 12 },
{ city: 'Moscow', temp: 18 },
{ city: 'Paris', temp: 20 },
{ city: 'London', temp: 14 }
];
groupBy(data, 'city');
/*
{
"Moscow": [
{ "city": "Moscow", "temp": 15 },
{ "city": "Moscow", "temp": 18 }
],
"London": [
{ "city": "London", "temp": 12 },
{ "city": "London", "temp": 14 }
],
"Paris": [
{ "city": "Paris", "temp": 20 }
]
}
*/groupBy.undefined.The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!