replace object keys javascript code example
Example 1: renemane object key js
obj = { name: 'Bobo' }
obj.newName = obj.name
delete obj.name
Example 2: javascript rename keys in object
const array = [
{ id: "1", name: "Apple" },
{ id: "2", name: "Tomato" },
{ id: "3", name: "Orange" },
{ id: "4", name: "Coconut" },
{ id: "5", name: "Strawberry" },
{ id: "6", name: "Banana" }
];
const newArray = array.map(item => {
return { itemId: item.id, itemName: item.name };
});
Example 3: js rename property
function renameProperty(obj, oldName, newName) {
obj[newName] = obj[oldName];
delete obj[oldName];
}
Example 4: js rename property
obj.newProperty = obj.property
delete obj.property