In this post, we are going to understand how to Rename keys in an array of objects Javascript in ES6 with examples. We have used the array.map() method and return an array of objects.
1. Rename keys in an array of objects Javascript in ES6
In this Javascript program, we have used array.map() method along with arrow function to rename keys that run for each object inside the array of objects and rename its key that we have assigned using Fullname: Empname, Department: Dep. But its return objects that contain only key we have replaced what if we have to maintain all key-value pairs.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
];
updatedarry = Employee.map(({Empname , Dep }) => ({ Fullname: Empname, Department: Dep }));
console.log(updatedarry)
Output
[
{ Fullname: 'John', Department: 'Admin' },
{ Fullname: 'jack', Department: 'IT' }
]
2. Rename keys in an array of objects Javascript in ES6
In this Javascript program, We have used the map() function to rename keys while maintaining all key-value pairs by using the spread operator.Finally used the foreach loop that iterates over the updated array(updatedarry) of the object and deletes the old property using the delete operator.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
];
updatedarry = Employee.map(item => { return { ...item, Department: item.Dep
}; });
updatedarry.forEach(object => {
delete object['Dep'];
});
console.log(updatedarry)
Output
[
{ Empname: 'John', salary: 60000, EmpID: 1, Department: 'Admin' },
{ Empname: 'jack', salary: 50000, EmpID: 2, Department: 'IT' }
]
3. Rename all properties of the object in ES6
Sometimes instead of changing single property, We have to change all properties of an array of objects by simply using the array.map() function and assign the old key to new key properties. Let us understand with an example.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
];
const updatedArr = Employee.map(Emp => {
return {Fullname: Emp.Empname, salary: Emp.salary,ID:Emp.EmpID,Department :Emp.Dep};
});
console.log(updatedArr)
Output
[
{ Fullname: 'John', salary: 60000, ID: 1, Department: 'Admin' },
{ Fullname: 'jack', salary: 50000, ID: 2, Department: 'IT' }
]
4. Rename all object keys using the function
In this javascript example, we have defined a function RenameallKeys() that takes an array of objects ( emparr) and keys (KeyToreplace) that we want to replace, and the Map() function run each object in an array of objects and foreach loop to rename the all old keys with new keys from an array of object.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
];
const RenameallKeys = (emparr, replaceKeys) => {
return emparr.map(emp => {
const renameObj = {};
Object.keys(emp).forEach(key => {
renameObj[replaceKeys[key]] = emp[[key]];
});
return renameObj;
});
};
const KeyToreplace = { EmpID: "Id", Empname: "Fullname",Dep :'Department',salary:'salary'};
const updatedarry = RenameallKeys(Employee, KeyToreplace);
console.log(updatedarry)
Output
[
{ Fullname: 'John', salary: 60000, Id: 1, Department: 'Admin' },
{ Fullname: 'jack', salary: 50000, Id: 2, Department: 'IT' }
]