9951 explained code solutions for 126 technologies


javascript-lodashHow do I use Lodash to group an array in JavaScript?


Lodash is a JavaScript library that provides utility functions for common programming tasks. It can be used to group an array in JavaScript with the _.groupBy() method.

Example

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

const groupedArray = _.groupBy(array, 'age');

console.log(groupedArray);

Output example

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

The code above uses Lodash's .groupBy() method to group the array of objects by age. The .groupBy() method takes two arguments: an array of values and a key to group by. It returns an object with the keys being the grouped values and the values being an array of objects with that key.

Code explanation

  1. const array = [...]; - declares an array of objects to be grouped.
  2. const groupedArray = _.groupBy(array, 'age'); - uses the _.groupBy() method to group the array by age.
  3. console.log(groupedArray); - logs the grouped array to the console.

Helpful links

Edit this code on GitHub