group-array-by

1.0.0 • Public • Published

group-array-by

Takes an array and returns an object with values grouped into arrays by passing each value to getKey to determine to which group the value belongs.

install

$ npm install group-array-by

example

const groupBy = require('group-array-by')
 
groupBy(v => v.type, [
  { type: 'coffee', roast: 'light' },
  { type: 'coffee', roast: 'medium' },
  { type: 'juice', fruit: 'grape' },
  { type: 'coffee', roast: 'dark' }
])
/* => {
  coffee: [
    { type: 'coffee', roast: 'light' },
    { type: 'coffee', roast: 'medium' },
    { type: 'coffee', roast: 'dark' }
  ],
  juice: [
    { type: 'juice', fruit: 'grape' }
  ]
*/

api

groupBy(getKey, array)

  • getKey(value)
    • value an item from the array
    • => key the key the item should be grouped under
  • array: [] the array to group into an object
  • => object the object of grouped values

Package Sidebar

Install

npm i group-array-by

Weekly Downloads

5

Version

1.0.0

License

CC0-1.0

Last publish

Collaborators

  • m59