Lodash – keyBy method

Lodash - keyBy method

Syntax Of Lodash keyBy method

_.keyBy(collection, [iteratee=_.identity])

Lodash keyBy 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 last element 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 = [
   { 'dir': 'left', 'code': 97 },
   { 'dir': 'right', 'code': 100 }
];
 
var result = _.keyBy(list, 'dir');
console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

{ left: { dir: 'left', code: 97 }, right: { dir: 'right', code: 100 } }

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply