🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

group-array

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

group-array

Group array of objects into lists.

0.1.1
Source
npm
Version published
Maintainers
2
Created
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

Package last updated on 19 Jul 2015

Did you know?

Socket

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