Lodash - invert method



Syntax

_.invert(object)

Creates an object composed of the inverted keys and values of object. If object contains duplicate values, subsequent values overwrite property assignments of previous values.

Arguments

  • object (Object) − The object to invert.

Output

  • (Object) − Returns the new inverted object.

Example

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

console.log(result);

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

Command

\>node tester.js

Output

{ '1': 'c', '2': 'b' }
lodash_object.htm
Advertisements