Last active
March 25, 2021 18:08
-
-
Save airglow923/aa3dc51957fa2aed98eed187097d905b to your computer and use it in GitHub Desktop.
Rename JavaScript Object key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const renameObjectKey = (object, oldName, newName) => { | |
// if newName is already present in object, replace it with oldName | |
if ( | |
oldName !== newName && | |
Object.prototype.hasOwnProperty.call(object, oldName) | |
) { | |
object[newName] = object[oldName]; | |
delete object[oldName]; | |
} | |
return object; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment