Group object keys and values into lists.
Install with npm
$ npm i group-object --save
var groupBy = require('group-object');
Create groupings from an object's keys and values.
Params
obj {Object}: Object to group.grouper {Function}: Optional grouping function that takes acc, value, key, obj, settersetter {Function}: Optional setter function that takes acc, 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'}
//=> }
//=> }
a.b.c) to get a nested value from an object.'a.b.c') paths.'a.b.c') paths.Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Brian Woodward
Copyright © 2015 Brian Woodward Released under the MIT license.
This file was generated by verb-cli on July 19, 2015.