Grouping objects by a property using reduce()

Posted by

Grouping Objects by a Property Using reduce()

The reduce() method is a powerful tool available in JavaScript that is used to iterate over an array and reduce it to a single value. It can also be used to group objects by a property. In this tutorial, we’ll explain how to do just that.

What is the reduce() Method?

The reduce() method is a function that is part of the JavaScript Array object. It is used to iterate over an array and return a single value. The syntax for reduce() is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

array.reduce(function(accumulator, currentValue, currentIndex, array), initialValue)

[/dm_code_snippet]

The function passed to reduce() is executed for each element of the array, starting from the beginning and ending with the last element. The function takes four arguments:

  • accumulator: The accumulator is the running total of the values returned by the reduce() function. It is the value that is returned by the previous iteration of the reduce() function.
  • currentValue: The currentValue is the current item in the array that the reduce() function is operating on.
  • currentIndex: The currentIndex is the index of the current item in the array.
  • array: The array is the array that the reduce() function is operating on.

The reduce() method can also take an optional initialValue parameter. This initialValue is used as the starting value for the accumulator. If no initialValue is provided, the first item in the array is used as the initialValue.

Grouping Objects by a Property

The reduce() method can be used to group objects by a property. To do this, we need to pass a function to the reduce() method that iterates over the array and groups the objects based on a property. The function should return an object with keys that represent the values of the property that is used to group the objects. The values associated with the keys should be an array containing the objects that have the corresponding value for the property.

For example, let’s say we have the following array of objects:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

const array = [
  {name: 'John', age: 21},
  {name: 'Jane', age: 22},
  {name: 'Bob', age: 21},
  {name: 'Alice', age: 23},
];

[/dm_code_snippet]

We can use the reduce() method to group these objects by their age property. The function that we pass to reduce() should look like this:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

array.reduce((acc, cur) => {
  if (!acc[cur.age]) {
    acc[cur.age] = [];
  }
  acc[cur.age].push(cur);
  return acc;
}, {});

[/dm_code_snippet]

The function takes the accumulator and current value as arguments. It checks if the accumulator has a key for the current object’s age property. If not, it creates an array with the key. Then it pushes the current object into the array associated with the age property. Finally, it returns the accumulator.

After running the reduce() method, we should get the following object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

{
  21: [{name: 'John', age: 21}, {name: 'Bob', age: 21}],
  22: [{name: 'Jane', age: 22}],
  23: [{name: 'Alice', age: 23}],
}

[/dm_code_snippet]

As you can see, the objects have been grouped by the age property. This is an example of how you can use the reduce() method to group objects by a property.

Conclusion

In this tutorial, we looked at how to use the reduce() method to group objects by a property. We discussed what the reduce() method is and how it works. We also looked at an example of how to use the reduce() method to group objects by a property. The reduce() method is a powerful tool that can be used to reduce an array to a single value or group objects by a property.