Skip to content

Instantly share code, notes, and snippets.

@sscovil
Created March 10, 2016 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sscovil/eca1176bc5113a8a2bc3 to your computer and use it in GitHub Desktop.
Save sscovil/eca1176bc5113a8a2bc3 to your computer and use it in GitHub Desktop.
A JavaScript function for renaming an object property.
function renameProperty(object, oldKey, newKey) {
if (object instanceof Object && object.hasOwnProperty(oldKey) && oldKey !== newKey) {
Object.defineProperty(object, newKey,
Object.getOwnPropertyDescriptor(object, oldKey));
delete object[oldKey];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment