Home > Web Front-end > JS Tutorial > How Can I Efficiently Group Objects in JavaScript Arrays Using a Custom `groupBy` Function?

How Can I Efficiently Group Objects in JavaScript Arrays Using a Custom `groupBy` Function?

DDD
Release: 2024-12-30 12:02:14
Original
132 people have browsed it

How Can I Efficiently Group Objects in JavaScript Arrays Using a Custom `groupBy` Function?

Grouping Objects in Arrays Effectively

Arrays can contain complex objects with multiple properties, making it necessary to efficiently group these objects based on specific criteria. Underscore.js provides a convenient groupby function, but it may not meet specific requirements.

To achieve SQL-like grouping, where values are merged rather than split, a custom approach is often preferred. One such implementation is presented below:

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};
Copy after login

This function accepts an array xs and a grouping key key. It iterates over the array elements, accumulating them into a result object rv. Each group is identified by the value of the key property.

To demonstrate, consider the following array of objects:

[ 
    { 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" },
    // ... (more objects)
]
Copy after login

Grouping by Phase yields:

console.log(groupBy(array, "Phase"));
// => {
//   "Phase 1": [
//     // Objects in Phase 1
//   ],
//   "Phase 2": [
//     // Objects in Phase 2
//   ]
// }
Copy after login

Grouping by Phase and Step combines values:

console.log(groupBy(array, "Phase", "Step"));
// => {
//   "Phase 1": {
//     "Step 1": [
//       // Objects in Phase 1, Step 1
//     ],
//     "Step 2": [
//       // Objects in Phase 1, Step 2
//     ]
//   },
//   "Phase 2": {
//     // Similar object structure for Phase 2
//   }
// }
Copy after login

This approach provides a flexible and efficient way to group objects in arrays. By implementing a custom groupBy function, you gain control over the grouping logic and can easily extend it to handle more complex scenarios.

The above is the detailed content of How Can I Efficiently Group Objects in JavaScript Arrays Using a Custom `groupBy` Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template