Skip to content

Instantly share code, notes, and snippets.

@ANovokmet
Created August 21, 2023 16:06
Show Gist options
  • Save ANovokmet/5bfa5e34d804fe0f2e437b67d81fe14d to your computer and use it in GitHub Desktop.
Save ANovokmet/5bfa5e34d804fe0f2e437b67d81fe14d to your computer and use it in GitHub Desktop.
Titleify all property values in an object
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