1.0.0 • Published 9 years ago
group-array-by v1.0.0
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-byexample
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)valuean item from the array=> keythe key the item should be grouped under
array: []the array to group into an object=> objectthe object of grouped values
1.0.0
9 years ago