Created
August 21, 2023 16:06
-
-
Save ANovokmet/5bfa5e34d804fe0f2e437b67d81fe14d to your computer and use it in GitHub Desktop.
Titleify all property values in an object
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
const obj = { | |
"assignedTo2StepDepartmentBy": "AssiGNed to depaRTment By" | |
}; | |
function toTitleCase(str) { | |
return str.replace( | |
/\w\S*/g, | |
function(txt) { | |
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); | |
} | |
); | |
} | |
function visit(obj) { | |
for(const key in obj) { | |
if(typeof obj[key] == 'object') { | |
visit(obj[key]); | |
} | |
else { | |
obj[key] = toTitleCase(obj[key]); | |
} | |
} | |
} | |
visit(obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment