Last active
May 8, 2024 14:31
-
-
Save ianmcnally/15c2b455a6b26a11ba2be678ba131766 to your computer and use it in GitHub Desktop.
Snake to camel case object keys in javascript
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
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