Home >Web Front-end >JS Tutorial >How Can I Efficiently Group Arrays of Objects in JavaScript?
When working with arrays of objects, it's often necessary to group them efficiently for analysis. Here's a discussion of the most effective approach to achieve this.
Using an External Library:
Underscore.js provides a convenient groupBy function that simplifies grouping by specific key values. However, it may not fully address the need for merging groups rather than splitting them.
Implementing a Custom GroupBy Function:
To achieve precise control over grouping and aggregation, consider implementing a custom function. Here's a vanilla JavaScript implementation:
var groupBy = function(xs, key) { return xs.reduce(function(rv, x) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); };
Example:
Let's apply this custom groupBy function to the example array:
var data = [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" } ]; var groupedByPhase = groupBy(data, "Phase"); var groupedByPhaseAndStep = groupBy(data, "Phase", "Step"); console.log(groupedByPhase); console.log(groupedByPhaseAndStep);
Output:
{ "Phase 1": [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" } ], "Phase 2": [ { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" } ] } { "Phase 1": { "Step 1": [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" } ], "Step 2": [ { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" } ] }, "Phase 2": { "Step 1": [ { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" } ], "Step 2": [ { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" } ] } }
As you can see, the results align with the expected format, grouping objects based on the specified key(s) while merging them rather than splitting them.
In conclusion, this custom implementation of groupBy provides efficient grouping capabilities for arrays of objects, fulfilling the desired aggregation functionality.
The above is the detailed content of How Can I Efficiently Group Arrays of Objects in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!