nest-by
Groups an array/object by property values or callback
Last updated 5 years ago by rstacruz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install nest-by 
SYNC missed versions from official npm registry.

nest-by

Groups an array/object by property values or callback

Works like group-by or lodash.groupBy, but also:

  • can nest recursively
  • works with objects as well as arrays

Status

Usage

nestBy(object or array, property, [...properties])

Arrays

list =
  [ { name: 'Marge', gender: 'f' },
    { name: 'Homer', gender: 'm' },
    { name: 'Bart', gender: 'm' } ]

result = nestBy(list, 'gender')
//  { f: [ { name: 'Marge', gender: 'f' } ],
//    m: [ { name: 'Homer', gender: 'm' },
//         { name: 'Bart', gender: 'm' } ] }

Objects

list =
  { marge: { name: 'Marge', gender: 'f' },
    homer: { name: 'Homer', gender: 'm' },
    bart: { name: 'Bart', gender: 'm' } }

var result = nestBy(list, 'gender')
// f:
//   marge: { name: 'Marge', gender: 'f' }
// m:
//   homer: { name: 'Homer', gender: 'm' }
//   bart: { name: 'Bart', gender: 'm' }

Recursive

list =
  [ { name: 'Marge', gender: 'f' },
    { name: 'Homer', gender: 'm' },
    { name: 'Bart', gender: 'm' } ]

result = nestBy(list, 'gender', 'name')
//  f:
//    'Marge':
//      [ { name: 'Marge', gender: 'f' } ]
//  m:
//    'Homer':
//      [ { name: 'Homer', gender: 'm' } ]
//    'Bart':
//      [ { name: 'Bart', gender: 'm' } ]

Thanks

nest-by © 2015+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (1)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |