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

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.

// ONLINE-RUNNER:browser;

function groupItems(array, property) {
    var reducer = function(groups, item) {
        var name = item[property]
        var group = groups[name] || (groups[name] = []);
        group.push(item);
        return groups;
    };
    return array.reduce(reducer, {});
}


// Usage example:

var array = [
    {name: 'John',  age: 25},
    {name: 'Kate',  age: 21},
    {name: 'Pablo', age: 25},
    {name: 'Denis', age: 25},
    {name: 'Chris', age: 31}
];

var groups = groupItems(array, 'age');  // array will be grouped by 'age' property


console.log('[key]\t[group]');

for(var key in groups) {
    var group = groups[key];
    console.log('"' + key + '"\t' + JSON.stringify(group));
}

 

2. Group by nested object property example

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.

// ONLINE-RUNNER:browser;

function getValue(object, path) {
    var reducer = function(tmp, key) {
        return tmp[key];
    };
    return path.reduce(reducer, object);
}

function groupItems(array, path) {
    var reducer = function(groups, item) {
        var name = getValue(item, path);
        var group = groups[name] || (groups[name] = []);
        group.push(item);
        return groups;
    };
    return array.reduce(reducer, {});
}


// Usage example:

var path = ['details', 'age'];  // array items will be grouped by `details.age`

var array = [
    {name: 'John',  details: {age: 25}},
    {name: 'Kate',  details: {age: 21}},
    {name: 'Pablo', details: {age: 25}},
    {name: 'Denis', details: {age: 25}},
    {name: 'Chris', details: {age: 31}}
];

var groups = groupItems(array, path);


console.log('[key]\t[group]');

for(var key in groups) {
    var group = groups[key];
    console.log('"' + key + '"\t' + JSON.stringify(group));
}

 

See also

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

References

  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
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