-
-
Save rlueder/a50b3837690060a5b175882099377e83 to your computer and use it in GitHub Desktop.
lodash helpers - rename (renames object keys)
This file contains 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
/* | |
* var person = { firstName: 'bill', lastName: 'johnson' } | |
* | |
* person = _.rename(person, 'firstName', 'first') | |
* person = _.rename(person, 'lastName', 'last') | |
* | |
* console.log(person) // { first: 'bill', last: 'johnson' } | |
*/ | |
_.rename = (obj, key, newKey) => { | |
if(_.includes(_.keys(obj), key)) { | |
obj[newKey] = _.clone(obj[key], true) | |
delete obj[key] | |
} | |
return obj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I modified it to handle dot syntax for deeply nested props: