Monday, July 27, 2026
HomeLanguagesJavascriptLodash _.invert() Method

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




// 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)); 


Output:

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

Example 2:

Javascript




// 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)); 


Output:

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

Example 3:  

Javascript




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


Output:

{neveropen: "Name", 
gfg@1234: "password",
your_neveropen: "username"}
RELATED ARTICLES

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6972 POSTS0 COMMENTS