How to invert key value in JavaScript object ?
Last Updated :
13 Jan, 2023
JavaScript is a high-level, interpreted, and dynamically typed client-side scripting language. JavaScript is used to add dynamic features to the static HTML. Everything is an object in JavaScript. Objects in JavaScript can be declared using figure brackets {..} and the objects may comprise certain properties. These properties are basically key-value pairs. The key is an identifier that is used to store and retrieve values. Inverting key-value pairs is tedious using conventional methods. But with the advent of "underscore.js", inversion of key values can be performed using the inbuilt method _.invert(). In this article, we shall discuss both methods of inverting key-value pairs of JavaScript objects.
First Approach: In this example, we will demonstrate the conventional method of inverting key-value pairs. At first, a student object is created with properties "name", "age", "std" and "fees". An inverse() function is defined which takes the student object as a parameter and loops through each key of the object. A new object retobj is defined which stores the inverted key-value pairs.
Example: This example shows the above-explained approach.
JavaScript
function inverse(obj){
var retobj = {};
for(var key in obj){
retobj[obj[key]] = key;
}
return retobj;
}
var student =
{
name : "Jack",
age: 18,
std : 12,
fees : 5000
}
console.log("Object before inversion");
console.log(student);
student = inverse(student);
console.log("Object after inversion");
console.log(student);
Output:
Object before inversion
{name: 'Jack', age: 18, std: 12, fees: 5000}
Object after inversion
{12: 'std', 18: 'age', 5000: 'fees', Jack: 'name'}
Second Approach: In this example, we use the _.invert() method of "underscore.js" to invert the key-value pairs of the object. The method takes the object as a parameter and returns a copy of the object with the keys as values and values as keys. The "student" object is passed to the _.invert() method. The method returns the inverted copy of the "student" object. The program imports the external "underscore.js" library to use inbuilt methods. The results are displayed on the webpage.
Syntax:
_.invert(object)
Example: This example shows the above-explained approach.
HTML
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/underscore-min.js">
</script>
<script type="text/javascript">
var student = {
name : "Jack",
age: 18,
std : 12,
fees : 5000
}
console.log("Object before inversion");
console.log(JSON.stringify(student));
student = JSON.stringify(_.invert(student));
console.log("Object after inversion");
console.log(student);
</script>
Output:
Object before inversion
{name: 'Jack', age: 18, std: 12, fees: 5000}
Object after inversion
{12: 'std', 18: 'age', 5000: 'fees', Jack: 'name'}
Similar Reads
How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to Iterate JSON Object in JavaScript? In JavaScript, there are different ways to iterate over the properties of a JSON object. Letâs look at the most common techniques.1. Using for...in LoopThe for...in loop is a simple way to go through the properties of a JSON object. It loops over the keys of the object. Inside the loop, we can acces
4 min read
How to add Key-Value pair to a JavaScript Object? A JavaScript object has a key-value pair, and it can be of variable length. We first need to declare an object and assign the values to that object for that there can be many methods.Below are the methods to add a key/value pair to a JavaScript object:Table of ContentUsing Dot NotationUsing Bracket
4 min read
How to Swap Array Object Values in JavaScript ? We have given the array of objects, and our task is to swap the values of the object keys present in the array of objects. Below is an example for a better understanding of the problem statement. Example:Input: arr = [{a: 1, b: 2}, {a:3, b: 4}]Output: [ { a: 2, b: 1 }, { a: 4, b: 3 } ]Explnation: Th
4 min read
How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table
4 min read