Skip to content

Instantly share code, notes, and snippets.

@ianmcnally
Last active May 8, 2024 14:31
Show Gist options
  • Save ianmcnally/15c2b455a6b26a11ba2be678ba131766 to your computer and use it in GitHub Desktop.
Save ianmcnally/15c2b455a6b26a11ba2be678ba131766 to your computer and use it in GitHub Desktop.
Snake to camel case object keys in javascript
function snakeToCamelObjectKeys(obj) {
return Object.keys(obj).reduce((output, key) => {
let value = obj[key]
const isObject = value.toString() === '[object Object]'
if (isObject) {
value = snakeToCamelObjectKeys(value)
}
const newKey = key.replace(/_(\w)/g, (match, $1) => $1.toUpperCase())
output[newKey] = value
return output
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment