Home

Awesome

group-object NPM version Build Status

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

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

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.