Grouping Array Elements by a Property in JavaScript
This challenge focuses on a common data manipulation task: grouping elements within an array based on the value of a specific property. This is a fundamental operation in data processing, often used for creating summaries, reports, or organizing data for display. Your task is to write a JavaScript function that efficiently groups an array of objects by a given property.
Problem Description
You are required to implement a function called groupBy that takes two arguments: an array of objects and a property name (string). The function should return a new object where:
- The keys of the object are the unique values of the specified property found in the input array.
- The values associated with each key are arrays containing all the objects from the input array that have that property value.
The function should handle cases where the input array is empty or the property does not exist on all objects. If the property doesn't exist on an object, that object should be placed in a group with the key "undefined".
Key Requirements:
- The function must be named
groupBy. - It must accept an array of objects and a property name (string) as input.
- It must return an object with grouped objects.
- The grouping should be based on the value of the specified property.
- Handle missing properties gracefully by grouping them under the key "undefined".
Expected Behavior:
The function should iterate through the input array, extract the value of the specified property for each object, and add the object to the corresponding array within the result object. If a property value is encountered for the first time, a new array should be created for that value.
Examples
Example 1:
Input:
[
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 30 },
{ name: 'David', age: 25 }
]
"age"
Output:
{
'25': [
{ name: 'Bob', age: 25 },
{ name: 'David', age: 25 }
],
'30': [
{ name: 'Alice', age: 30 },
{ name: 'Charlie', age: 30 }
]
}
Explanation: Objects are grouped by their 'age' property. Objects with age 25 are in the '25' group, and objects with age 30 are in the '30' group.
Example 2:
Input:
[
{ name: 'Alice', age: 30 },
{ name: 'Bob' },
{ name: 'Charlie', age: 30 }
]
"age"
Output:
{
'30': [
{ name: 'Alice', age: 30 },
{ name: 'Charlie', age: 30 }
],
'undefined': [
{ name: 'Bob' }
]
}
Explanation: Bob doesn't have an 'age' property, so he's grouped under the 'undefined' key.
Example 3:
Input:
[]
"age"
Output:
{}
Explanation: An empty array results in an empty object.
Constraints
- The input array will contain objects.
- The property name will be a string.
- The input array can be empty.
- Objects in the array may or may not have the specified property.
- The function should have a time complexity of O(n), where n is the number of elements in the input array. Avoid nested loops that would increase the complexity.
Notes
Consider using a for...of loop or reduce method for efficient iteration. The hasOwnProperty method can be helpful for checking if an object has a specific property. Think about how to initialize the grouped object and add elements to the appropriate arrays. Don't mutate the original array.