In this post, we are going to learn how to swap key-value of object in JavaScript by using a for-loop, object.entries(),map() reverse() method,object.keys(),reduce(),assign() function in Javascript.
1. for-in loop to swap key-value of object in JavaScript
The for-loop is used to iterate over key-value pairs in Javascript objects. In this Javascript program, we are using it to iterate over key-value pair of objects and swap keys and display the result.
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
var swapObj = {};
for(var key in Employee)
{
swapObj[Employee[key]] = key;
}
console.log(swapObj)
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
- How to get json object key value in JavaScript
- Check object has property in JavaScript
- Add multiple properties to array of object in JavaScript
- Delete property from objects Array in Javascript(6 ways)
- 7 ways to Remove object from array in Javascript
2. object. entries() to swap key-value of object in javascript
The object entries() method returns separate arrays for keys and values of the object. The map() function is used to apply a function on each key-value pair of objects We have applied this function ([key, value]) => ({[value]: key}). The javascript object.assign() copies enumerate properties of one or more sources to the target source and return the target object using the spread operator.
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
var swapObj = {};
const swapped = Object.entries(Employee).map(
([key, value]) => ({[value]: key})
);
swapobj = Object.assign({}, ...swapped);
console.log(swapobj)
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
3.object.entries() to swap key values in javascript
The object entries() method returns separate arrays for keys and values of the object. The map() function is used to apply a function on each key-value pair of objects We have applied this function ([key, value]) => ({[value]: key}).
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
function SwapObjkeyValue(Emp) {
const SwapObj = Object.entries(Emp).map(
([key, value]) => [value, key]
);
return Object.fromEntries(SwapObj );
}
console.log(SwapObjkeyValue(Employee))
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
4. reverse() to Swap object key-value in JavaScript
The object entries() method returns separate arrays for keys and values of the object. The map() function is used to apply a function on each key-value pair of objects We have applied this function ([key, value]) => ({[value]: key}).The reverse() method to reverse key=value pair.
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
const swapObj = Object.fromEntries(Object.entries(Employee).map(Emp => Emp.reverse()))
console.log(swapObj)
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
5. Foreach loop to Swap object key-value in JavaScript
In this Javascript program, First, we have got keys of the object as an array using object.keys() method and iterate over the keys of an object using the foreach loop and iterate over each key-value pair and swapped it.
let Employee = {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'};
const swapobject = {};
Object.keys(Employee).forEach(key => {
swapobject[Employee[key]] = key;
});
console.log(swapobject)
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
6. assign() and reduce to Swap key-value of object in JavaScript
In this javascript program, we get an array of keys using object.keys() method and have used reduce() method to swap key and value pair and assign() method to create a new object of swap key-value pair and return it.
let Employee = {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
const SwamObj = Object.keys(Employee).reduce(function(obj, key) {
obj[Employee[key]] = key;
return obj;
}, {});
console.log(SwamObj);
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
7. object.entries() and reduce() to Swap key-value pair
In this JavaScript program, we have used object.entries() method to get separate arrays for keys and values of a given object. The reduce() method is to iterate over the arrays and returns an object of swap key-value pair. Let us understand with the below example.
let Employee = {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'};
function SwapObjkeyValue(obj) {
return Object.entries(obj).reduce((obj, entry) => {
const [ key, value ] = entry;
obj[ value ] = key;
return obj;
}, {});
}
console.log(SwapObjkeyValue(Employee));
Output
{ '1': 'EmpID', '60000': 'salary', John: 'Empname', Admin: 'Dep' }
Summary
In this post, we are going to learn how to swap object key-value in JavaScript with examples by using a for-loop, object.entries(),map() reverse() method,object.keys(),reduce(),assign() function in Javascript.