- Lodash Tutorial
- Lodash - Home
- Lodash - Overview
- Lodash - Environment Setup
- Lodash - Array
- Lodash - Collection
- Lodash - Date
- Lodash - Function
- Lodash - Lang
- Lodash - Math
- Lodash - Number
- Lodash - Object
- Lodash - Seq
- Lodash - String
- Lodash - Util
- Lodash - Properties
- Lodash - Methods
- Lodash Useful Resources
- Lodash - Quick Guide
- Lodash - Useful Resources
- Lodash - Discussion
Lodash - groupBy method
Syntax
_.groupBy(collection, [iteratee=_.identity])
Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).
Arguments
collection (Array|Object) − The collection to iterate over.
[iteratee=_.identity] (Function) − The iteratee to transform keys.
Output
(Object) − Returns the composed aggregate object.
Example
var _ = require('lodash'); var list = [1.1 ,1.4, 2.1, 2.3]; var result = _.groupBy(list, Math.floor); console.log(result); result = _.groupBy(['one', 'two', 'three'], 'length'); console.log(result);
Save the above program in tester.js. Run the following command to execute this program.
Command
\>node tester.js
Output
{ '1': [ 1.1, 1.4 ], '2': [ 2.1, 2.3 ] } { '3': [ 'one', 'two' ], '5': [ 'three' ] }
lodash_collection.htm
Advertisements