how to change the key name of an object in javascript 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: 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 3: 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: label, name: value }) => ({ label, value }));
console.log(transformed);

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

Example 6: 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!