
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to invert an object in JavaScript?
Invert means to print the value in reverse order. In a technical word, we can say that suppose we have taken an object of the array. The object takes the value in key-value pairs and prints the value in value-key pairs.
The _.invert() function belongs to the underscore.js a javascript library that provides versatile functions.
This is used to copy an object where the object key is converted into value and the object is converted into the key. This means the [key, value] of the object is reversed.
Syntax
Following is the syntax of the _.invert() function −
_.invert(object)
Object − Object is the key-value pairs of an array.
Return − it returns the [key, value] of an object in reverse order [value,key].
Example: 1
In the following example we are creating an object named obj, passing the obj var in the _.invert() function, and returning the value in value−key pairs.
<!DOCTYPE html> <html> <head> <script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script> </head> <body> <script type="text/javascript"> var obj = { Company: "TurorialsPoint", Address: "Hyderabad", Contact: "+917654980321", Email: "xyz123@gmail.com", }; document.write(JSON.stringify(_.invert(obj))); </script> </body> </html>
Example 2
In the following example, we are passing an object named obj to the _.invert() function and converting it into the string to display the return data on the page.
<!DOCTYPE html> <html> <head> <script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script> </head> <body> <script type="text/javascript"> var obj = { num1: "91", num2: "82", num3: "52", num4: "19", }; var number = JSON.stringify(_.invert(obj)); document.write(number); </script> </body> </html>
Example 3
Let us see another example of the _.invert() function −
<!DOCTYPE html> <html> <head> <script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script> </head> <body> <script type="text/javascript"> var detail = JSON.stringify( _.invert({ name: "JP ANNA", age: 324, salary: 220000 }) ); document.write(detail); </script> </body> </html>
Example 4: Using the arrow(=>) function
In this example, we are going to discuss the usage of arrow function. In this case, we will use the => arrow function to reverse the key/value pair of an object.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <scripttype="text/javascript"src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script> <title>Invert an object</title> <div id="invert"></div> </head> <body> <script type="text/javascript"> let obj = { name: "Alice", age: 21, role: "Full stack developer", }; let invertObj = (obj) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key])); document.getElementById("invert").innerHTML = JSON.stringify( invertObj(obj) ); </script> </body> </html>
Example 5: Using the reverse() method
In this example, we are going to invert an object in JavaScript using the reverse() method. This method is used to reverse the key/value pair of an object.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <scripttype="text/javascript"src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script> <title>Invert an object</title> <div id="invert"></div> </head> <body> <script type="text/javascript"> let obj = { name: "Alice", age: 21, role: "Full stack developer", }; function invertObj(obj) { let invert_obj = {}; reverse_obj = Object.keys(obj).reverse(); reverse_obj.forEach(function (x) { invert_obj[x] = obj[x]; }); return invert_obj; } rev = invertObj(obj); document.getElementById("invert").innerHTML = JSON.stringify(rev); </script> </body> </html>
- Related Articles
- How to freeze an Object in JavaScript?
- How to clone an object in JavaScript?
- How to access an object through another object in JavaScript?
- How to invert the colors of an image in Node Jimp?
- How to convert an object into an array in JavaScript?
- How to Invert the color of an image using JavaFX?
- How to remove an object using filter() in JavaScript?
- How to define methods for an Object in JavaScript?
- How to de-structure an imported object in JavaScript?
- How to create an object with prototype in JavaScript?
- How to check whether an object exists in JavaScript?
- How to add an element to a javascript object?
- How to get the length of an object in JavaScript?
- How to get the values of an object in JavaScript?
- How to delete a property of an object in JavaScript?
