JSPM

groupby-array

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q32488F
  • License MIT

Groups an array by the specified property

Package Exports

  • groupby-array
  • groupby-array/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (groupby-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

groupby-array

Groups an array by the specified property.

NPM Version Action Status Coverage Status License

Installation

yarn add groupby-array

or

npm install --save groupby-array

Usage

Basic usage

import { groupBy } from 'groupby-array';

const users = [
  { name: 'John', age: 30, isAdmin: true },
  { name: 'Jane', age: 25, isAdmin: true },
  { name: 'Jim', age: 30, isAdmin: false }
];
  
const result = groupBy(users, u => u.age);
// result

{
  30: [
    { name: 'John', age: 30, isAdmin: true },
    { name: 'Jim', age: 30, isAdmin: false }
  ],
  25: [
    { name: 'Jane', age: 25, isAdmin: true }
  ]
};

Group by multiple properties

const users = [
  { firstName: 'John', lastName: 'Doe', age: 30 },
  { firstName: 'Jane', lastName: 'Doe', age: 25 },
  { firstName: 'James', lastName: 'Bond', age: 30 }
];
  
const result = groupBy(users, u => `${u.firstName} ${u.lastName}`);
// result

{
  'John Doe': [
    { firstName: 'John', lastName: 'Doe', age: 30 }
  ],
  'Jane Doe': [
    { firstName: 'Jane', lastName: 'Doe', age: 25 }
  ],
  'James Bond': [
    { firstName: 'James', lastName: 'Bond', age: 30 }
  ]
};

More examples can be found in the tests