how to change object key in javascript code example

Example 1: renemane object key js

obj = { name: 'Bobo' } //key: value
obj.newName = obj.name // on object create new key name. Assign old value to this
delete obj.name //delete object with old key 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 };
});

// 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" }
// ];

Example 3: js change key value in object

var object = { boo:true, baa:5 };
console.log(object);
function change() {
  object.boo = false;
  object.baa++;
};
change();
console.log(object);
//Hope this helps!

Example 4: js rename property

function renameProperty(obj, oldName, newName) {
  obj[newName] = obj[oldName];
  delete obj[oldName];
}

Example 5: js rename property

obj.newProperty = obj.property
delete obj.property