Socket
Socket
Sign inDemoInstall

group-array

Package Overview
Dependencies
10
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    group-array

Group array of objects into lists.


Version published
Weekly downloads
78K
decreased by-6.52%
Maintainers
2
Install size
71.3 kB
Created
Weekly downloads
 

Readme

Source

group-array NPM version Build Status

Group array of objects into lists.

Install with npm

$ npm i group-array --save

Usage

var groupArray = require('group-array');

API

groupBy

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);
//=> {
//=>   one: [
//=>     { group: 'one', content: 'A'},
//=>     { group: 'one', content: 'B'}
//=>   ],
//=>   two: [
//=>     { group: 'two', content: 'C'},
//=>     { group: 'two', content: 'D'}
//=>   ],
//=>   three: [
//=>     { group: 'three', content: 'E'},
//=>     { group: 'three', content: 'F'}
//=>   ]
//=> }
  • 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.

FAQs

Last updated on 19 Jul 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc