Lodash _.invert() Method

The _.invert() method is used to return the copy of an object where the object key is converted into value and object value is converted into the key. If object contains duplicate values, subsequent values overwrite property assignments.

Syntax:

_.invert(object)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • object: This parameter holds the object to invert.

Return Value: This method returns the new inverted object.

Example 1:



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var obj =  { 'a': 1, 'b': 2, 'c': 3 };
  
// Use of _.invert method 
console.log(_.invert(obj)); 

chevron_right


Output:

{ '1': 'a', '2': 'b', '3': 'c'}

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var obj =  { 'a': 1, 'b': 2, 'c': 2 };
  
// Use of _.invert method 
console.log(_.invert(obj)); 

chevron_right


Output:

{ '1': 'a', '2': 'c' }

Example 3:  

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

/// Requiring the lodash library  
const _ = require("lodash");  
   
// Given object
var obj = { 
            Name: "GeeksforGeeks"
            password: "gfg@1234"
            username: "your_geeks"
        
   
// Use of _.invert method 
console.log(_.invert(obj));

chevron_right


Output:

{GeeksforGeeks: "Name", 
gfg@1234: "password",
your_geeks: "username"}

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.