Rename object keys

JavaScript, Object · Oct 22, 2020

Replaces the names of multiple object keys with the values provided.

const renameKeys = (keysMap, obj) =>
  Object.keys(obj).reduce(
    (acc, key) => ({
      ...acc,
      ...{ [keysMap[key] || key]: obj[key] }
    }),
    {}
  );
const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 };
renameKeys({ name: 'firstName', job: 'passion' }, obj);
// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }

More like this

  • JavaScript Object Key Transformations

    Easily transform object keys to your liking with this snippet collection.

    Collection · 6 snippets

  • Find matching keys

    Finds all the keys in the provided object that match the given value.

    JavaScript, Object · Nov 15, 2020

  • Map object keys

    Maps the keys of an object using the provided function, generating a new object.

    JavaScript, Object · Oct 21, 2020

  • Map object values

    Maps the values of an object using the provided function, generating a new object with the same keys.

    JavaScript, Object · Oct 21, 2020