How to swap key and value of JSON element using JavaScript ? Last Updated : 21 Jul, 2023 Summarize Comments Improve Suggest changes Share 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 Like