group-object

Group object keys and values into lists.
Install with npm
$ npm i group-object --save
Usage
var groupBy = require('group-object');
API
groupBy
Create groupings from an object's keys and values.
Params
obj{Object}: Object to group.grouper{Function}: Optional grouping function that takesacc, value, key, obj, settersetter{Function}: Optional setter function that takesacc, group, value, key, objacc{Object}: Optional accumulator object passed to the setter function.returns{Object}: Object containing groups as keys and list of objects as values in the group.
Example
function grouper (acc, value, key, obj, setter) {
return value.group;
}
function setter (acc, group, value, key, obj) {
acc[group] = acc[group] || {};
acc[group][key] = value;
}
var obj = {
a: { group: 'one', content: 'A'},
b: { group: 'one', content: 'B'},
c: { group: 'two', content: 'C'},
d: { group: 'two', content: 'D'},
e: { group: 'three', content: 'E'},
f: { group: 'three', content: 'F'}
};
var groups = groupBy(obj, grouper, setter);
//=> {
//=> one: {
//=> a: { group: 'one', content: 'A'},
//=> b: { group: 'one', content: 'B'}
//=> },
//=> two: {
//=> c: { group: 'two', content: 'C'},
//=> d: { group: 'two', content: 'D'}
//=> },
//=> three: {
//=> e: { group: 'three', content: 'E'},
//=> f: { group: 'three', content: 'F'}
//=> }
//=> }
Related projects
- for-in: Iterate over the own and inherited enumerable properties of an objecte, and return an object… more
- group-array: Group array of objects into lists.
- get-value: Use property paths (
a.b.c) to get a nested value from an object. - sort-object: Sort the keys in an object.
- set-value: Create nested values and any intermediaries using dot notation (
'a.b.c') paths. - upsert-value: Update or set nested values and any intermediaries with dot notation (
'a.b.c') paths.
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.