Languages
[Edit]
EN

JavaScript - group / grouping array items with reduce method

4 points
Created by:
Creg
9600

In this article, we're going to have a look at how to group array items with Array reduce() method. This article shows two ways to use Array reduce method to group array by item property. Second one example shows how to group it with nested properties.

1. Group by object property example

Edit

This section contains simple grouping of the array by property. Array reduce method in below example takes two arguments:

  • function that converts array structure,
  • empty object that will be filled with groups.

To change group name it is necessary only to change property variable.

 

2. Group by nested object property example

Edit

In this section first example was modiffied to provide grouping by nested property.

To change group name it is necessary only to change path array to own one.

 

See also

Edit
  1. JavaScript - merge objects in array with the same keys

References

Edit
  1. Array.prototype.reduce() - JavaScript | MDN

Alternative titles

  1. JavaScript - group / grouping array items with reduce method by nested property
  2. JavaScript - group by on array of objects
  3. JavaScript - group / grouping objects in array with reduce method
  4. JavaScript - break array of objects into separate arrays based on property
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join