group-array
Group array of objects into lists.
Install with npm
$ npm i group-array --save
Usage
var groupArray = require('group-array');
API
Create groupings from array's values.
Params
arr
{Array}: Array to group.grouper
{Function}: Optional grouping function that takes acc, value, i, arr, setter
setter
{Function}: Optional setter function that takes acc, group, value, i, arr
acc
{Object|Array}: Optional accumulator object or array passed to the setter function.returns
{Object|Array}: Object or array containing groups as keys and list of objects as values in the group.
Example
function grouper (acc, value, i, arr, setter) {
return value.group;
}
function setter (acc, group, value, i, arr) {
acc[group] = acc[group] || [];
acc[group].push(value);
}
var arr = [
{ group: 'one', content: 'A'},
{ group: 'one', content: 'B'},
{ group: 'two', content: 'C'},
{ group: 'two', content: 'D'},
{ group: 'three', content: 'E'},
{ group: 'three', content: 'F'}
];
var groups = groupBy(arr, grouper, setter);
Related projects
- arr-reduce: Fast array reduce that also loops over sparse elements.
- group-object: Group object keys and values into lists.
- get-value: Use property paths (
a.b.c
) to get a nested value from an object. - union-value: Set an array of unique values as the property of an object. Supports setting deeply… more
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Brian Woodward
License
Copyright © 2015 Brian Woodward
Released under the MIT license.
This file was generated by verb-cli on July 19, 2015.