The _.invert() function 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. It means the [key, value] of the object is reverse.
Syntax:
_.invert( object )
Parameters: This function accepts single parameter as mentioned above and described below:
- object: It contains the object element that holds the elements of key and value pair.
Return Value: It returns the [key, value] of an object in reverse [value, key] order.
Example 1:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" Â Â Â Â Â Â Â Â Â Â Â Â Â src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â Â Â var obj = { Â Â Â Â Â Â Â Â Â Â Â Â Company: "neveropen", Â Â Â Â Â Â Â Â Â Â Â Â Address: "Noida", Â Â Â Â Â Â Â Â Â Â Â Â Contact: "+91 9876543210", Â Â Â Â Â Â Â Â Â Â Â Â Email: "abc@gfg.com" Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â console.log(_.invert(obj)); Â Â Â Â </ script > </ body > Â Â </ html > |
Output:
Example 2:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" Â Â Â Â Â Â Â Â Â Â Â Â Â src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â Â Â var inv = _.invert({ Â Â Â Â Â Â Â Â Â Â Â Â num1: 10, Â Â Â Â Â Â Â Â Â Â Â Â num2: 20, Â Â Â Â Â Â Â Â Â Â Â Â num3: 30, Â Â Â Â Â Â Â Â Â Â Â Â num4: 40 Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â Â Â console.log(inv); Â Â Â Â </ script > </ body > Â Â </ html > |
Output: