Lodash - invertBy method


Advertisements

Syntax

_.invertBy(object, [iteratee=_.identity])

This method is like _.invert except that the inverted object is generated from the results of running each element of object thru iteratee. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. The iteratee is invoked with one argument: (value).

Arguments

  • object (Object) − The object to invert.

  • [iteratee=_.identity] (Function) − The iteratee invoked per element.

Output

  • (Object) − Returns the new inverted object.

Example

var _ = require('lodash');
var object = { 'a': 1, 'b': 2, 'c': 1 };
var result = _.invertBy(object);
console.log(result);

result = _.invertBy(object, function(value) {
   return 'group' + value;
});
console.log(result);

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

Command

\>node tester.js

Output

{ '1': [ 'a', 'c' ], '2': [ 'b' ] }
{ group1: [ 'a', 'c' ], group2: [ 'b' ] }
lodash_object.htm
Advertisements