简体   繁体   中英

Renaming object keys in JS

I want to rename some keys of an object according to a mapping defined in another object. I have a code segment like this, but this does not seem to work, meaning does not rename the object keys:

 const test = { id: 5, text: 'Plain text', time_created: new Date(), }; const replacements = { id: 'userId', time_created: 'postedAt', }; console.log(test); function renameObjectKeys(obj, replacements) { Object.entries(obj, ([key, _]) => { if (key in Object.keys(replacements)) { if (key !== replacements[key]) { Object.defineProperty(obj, replacements[key], Object.getOwnPropertyDescriptor(obj, key)); delete obj[key]; } } }); } renameObjectKeys(test, replacements); console.log(test); 

How can I achieve what I want?

I've detected two issues in your code:

  1. In the Object.entries line, you're trying to do something like a forEach giving a predicate to the whole function, and Object.entries doesn't support your use case. So you should use Array#forEach .

  2. In the line of if (key in Object.keys(replacements)) you're misunderstanding in : this operator works on objects to determine if a given property is part of the own object or somewhere in the entire prototype chain. Thus, you should use in on replacements directly.

 const test = { id: 5, text: 'Plain text', time_created: new Date(), }; const replacements = { id: 'userId', time_created: 'postedAt', }; console.log(test); function renameObjectKeys(obj, replacements) { Object.entries(obj).forEach(([key, _]) => { // <---- (1) if (key in replacements) { // <---- (2) if (key !== replacements[key]) { Object.defineProperty(obj, replacements[key], Object.getOwnPropertyDescriptor(obj, key)); delete obj[key]; } } }); } renameObjectKeys(test, replacements); console.log(test); 

Safer approach: don't mutate

There's a simpler approach which doesn't involve renaming these properties in place, but creating a new object without mutating the input one:

 const test = { id: 5, text: 'Plain text', time_created: new Date(), } const replaces = { id: 'userId', time_created: 'postedAt', } // Converts entries to an object back again const fromEntries = entries => entries.reduce ((o, [key, value]) => ({ ...o, [key]: value }), {}) const renameProps = (replaces, obj) => fromEntries ( Object.entries (obj) .map (([key, value]) => [ replaces.hasOwnProperty (key) ? replaces[key] : key, value ]) ) const renamed = renameProps (replaces, test) console.log ('from ', test) console.log ('to ', renamed) 

Try this,

const test = {
  id: 5,
  text: 'Plain text',
  time_created: new Date(),
};

const replacements = {
  id: 'userId',
  time_created: 'postedAt',
};

console.log("test",test);

function renameObjectKeys(obj, replacements) {
    for (var key in replacements)
    {
        var temp = test[key];
        delete test[key];
        test[replacements[key]] = temp;
    }
}

renameObjectKeys(test, replacements);
console.log("test",test);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM