Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES6 module to recursively convert snake case keys in an object to camel case using lodash.
export function camelCaseKeys(object) {
let camelCaseObject = _.cloneDeep(object)
if (_.isArray(camelCaseObject)) {
return _.map(camelCaseObject, camelCaseKeys)
}
if (_.isString(camelCaseObject)) {
return camelCaseObject
}
camelCaseObject = _.mapKeys(camelCaseObject, (value, key) => _.camelCase(key))
// Recursively apply throughout object
return _.mapValues(camelCaseObject, (value) => {
if (_.isPlainObject(value)) {
return camelCaseKeys(value)
} else if (_.isArray(value)) {
return _.map(value, camelCaseKeys)
}
return value
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.