how to change key name of object js code example
Example 1: change object key name javascript es6
const countries = [
{"id": 1, "name": "Afghanistan"},
{"id": 2, "name": "Albania"},
{"id": 3, "name": "Algeria"},
{"id": 4, "name": "American Samoa"}
];
const transformed = countries.map(({ id, name }) => ({ label: id, value: name }));
console.log(transformed);
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 };
});