Syntax Lodash count By method
_.countBy(collection, [iteratee=_.identity])
Lodash count By method creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by the iteratee.
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, 2.1, 2.3] var result = _.countBy(list, Math.floor); 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, '2': 2 }
Next Topic – Click Here
Pingback: Lodash - Collection - Adglob Infosystem Pvt Ltd