JavaScript, Object

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

  • Use Object.keys() in combination with Array.prototype.reduce() and the spread operator (...) to get the object's keys and rename them according to keysMap.
const renameKeys = (keysMap, obj) =>
    (acc, key) => ({
      ...{ [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 }

Recommended snippets

  • Have you ever wanted to serialize an object but only include certain keys? Turns out JavaScript provides an easy way to do this!

  • mapValues

    JavaScript, Object

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

  • findKeys

    JavaScript, Object

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