https://github.com/doowb/group-object
Group object keys and values into lists.
https://github.com/doowb/group-object
Last synced: about 1 month ago
JSON representation
Group object keys and values into lists.
- Host: GitHub
- URL: https://github.com/doowb/group-object
- Owner: doowb
- License: mit
- Created: 2015-07-10T13:28:56.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-16T02:59:39.000Z (over 9 years ago)
- Last Synced: 2025-02-26T19:51:44.830Z (about 2 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# group-object [](http://badge.fury.io/js/group-object) [](https://travis-ci.org/doowb/group-object)
> Group object keys and values into lists.
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i group-object --save
```## Usage
```js
var groupBy = require('group-object');
```## API
### [groupBy](index.js#L62)
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, setter`
* `setter` **{Function}**: Optional setter function that takes `acc, group, value, key, obj`
* `acc` **{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**
```js
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](https://github.com/jonschlinkert/for-in): Iterate over the own and inherited enumerable properties of an objecte, and return an object… [more](https://github.com/jonschlinkert/for-in)
* [group-array](https://github.com/doowb/group-array): Group array of objects into lists.
* [get-value](https://github.com/jonschlinkert/get-value): Use property paths (` a.b.c`) to get a nested value from an object.
* [sort-object](https://github.com/doowb/sort-object): Sort the keys in an object.
* [set-value](https://github.com/jonschlinkert/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.
* [upsert-value](https://github.com/doowb/upsert-value): Update or set nested values and any intermediaries with dot notation (`'a.b.c'`) paths.## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/group-object/issues/new)
## Author
**Brian Woodward**
+ [github/doowb](https://github.com/doowb)
+ [twitter/doowb](http://twitter.com/doowb)## License
Copyright © 2015 Brian Woodward
Released under the MIT license.***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 19, 2015._