How to swap key and value of JSON element using JavaScript ? Last Updated : 21 Jul, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will swap the key and value of JSON elements using JavaScript. Given a JSON object and the task is to swap the JSON object key with values and vice-versa with the help of JavaScript. Below are the following approaches: Using for loopUsing Object.keys() and ForEach() MethodUsing Object.entries() and map() MethodApproach 1: Using for loopCreate a new empty object.Visit every key of an object by for loop and add the elements from the old object to the new object in reverse form(by swapping the key and values).Example: This example implements the above approach. JavaScript let obj = { "Prop_1": "Val_1", "Prop_2": "Val_2", "Prop_3": "Val_3" }; function swapValues(j) { let res = {}; for (let key in j) { res[j[key]] = key; } return res; } function GFG_Fun() { console.log(JSON.stringify(swapValues(obj))); } GFG_Fun(); Output{"Val_1":"Prop_1","Val_2":"Prop_2","Val_3":"Prop_3"}Approach 2: Using Object.keys() and ForEach() MethodCreate a new empty object.For each key of the object, add the elements from the old object to the new object in reverse form (by swapping the key and values). Example: This example implements the above approach. JavaScript let obj = { "Prop_1": "Val_1", "Prop_2": "Val_2", "Prop_3": "Val_3" }; function swapValues(o) { const res = {}; Object.keys(o).forEach(key => { res[o[key]] = key; }); return res; } function GFG_Fun() { console.log(JSON.stringify(swapValues(obj))); } GFG_Fun(); Output{"Val_1":"Prop_1","Val_2":"Prop_2","Val_3":"Prop_3"}Approach 3: Using Object.entries() and map() MethodIn this method, we will use Object.entries() and map() Methods to get all the key values of the object in the array format. Example: JavaScript let obj = { "Prop_1": "Val_1", "Prop_2": "Val_2", "Prop_3": "Val_3" }; const swappedObject = Object.entries(obj).reduce((acc, [key, value]) => { acc[value] = key; return acc; }, {}); console.log(swappedObject); Output{ Val_1: 'Prop_1', Val_2: 'Prop_2', Val_3: 'Prop_3' } Comment More infoAdvertise with us Next Article How to swap key and value of JSON element using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-object JSON JavaScript-Questions +1 More Similar Reads How to Swap Two Array of Objects Values using JavaScript ? Swapping values between two arrays of objects is a common operation in JavaScript, there are multiple methods to swap values between arrays of objects efficiently. Swapping values between arrays can be useful in scenarios like reordering data, shuffling items, or performing certain algorithms. These 6 min read How to create an object from the given key-value pairs using JavaScript ? In this article, we will learn to create an object from the given key-value pairs using JavaScript.Key-value pair: Unique identifier (key) associated with its corresponding value; fundamental for data organization and retrieval in programming. here we have some common approaches.We will explore the 5 min read How to Convert HTML Form Field Values to JSON Object using JavaScript? Storing HTML input data into JSON format using JavaScript can be useful in various web development scenarios such as form submissions, data processing, and AJAX requests. Here we will explore how to convert the HTML form input data into the JSON format using JavaScript. ApproachThe HTML file contain 2 min read Swapping two array elements in a single line using JavaScript In JavaScript, there exist many ways by which one can swap two array elements. In this article, we will discuss a way in which one can swap two array elements in JavaScript in a single line. The input and output would be as follows. Input: arr = { 10, 20, 40, 30 }Output: arr = { 10, 20, 30, 40 } // 3 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 Variables using Destructuring Assignment in JavaScript? Destructuring assignment in JavaScript allows us to unpack the values from arrays or objects and assign them to multiple variables.Syntax// Destructuring from Array, arr = [ 10, 15 ]let [ a, b ] = arr; // a=> 10, b => 15// Destructuring from Array, obj = { n1: 10, n2: 15 ]let [ n1, n2 ] = obj; 2 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 Convert XML to JSON using JavaScript Convert XML to JSON effortlessly using JavaScript, enabling seamless integration and manipulation of XML data within your applications. JavaScript libraries like xml-js and xmldom Library simplify the conversion process, ensuring compatibility and efficiency. Below are the approaches to convert XML 2 min read How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort 3 min read How to remove a key-value pair from JavaScript object? Removing a key-value pair from an object in JavaScript means deleting a specific property and its corresponding value from the object. This can be achieved using the delete operator, destructuring with the rest operator, or various utility functions to manipulate the object dynamically.How to remove 3 min read Like