Created
December 16, 2021 18:25
-
-
Save fsjorgeluis/f420f139bd912bc7669536e76f208825 to your computer and use it in GitHub Desktop.
Rename 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
export const renameKey = (object: any, key: string, newKey: string) => { | |
const clonedObj: Record<string, unknown> = clone(object); | |
const targetKey = clonedObj[key]; | |
delete clonedObj[key]; | |
clonedObj[newKey] = targetKey; | |
return clonedObj; | |
}; | |
const clone = (object) => Object.assign({}, object); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment