how to rename object key in javascript in a loop code example
Example: 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 };
});
// Result:
// [
// { itemId: "1", itemName: "Apple" },
// { itemId: "2", itemName: "Tomato" },
// { itemId: "3", itemName: "Orange" },
// { itemId: "4", itemName: "Coconut" },
// { itemId: "5", itemName: "Strawberry" },
// { itemId: "6", itemName: "Banana" }
// ];