Skip to content

Instantly share code, notes, and snippets.

@kariudo
Created May 4, 2017 20:03
Show Gist options
  • Save kariudo/6cb59eac1e1344c053c7fc4d3d919c5e to your computer and use it in GitHub Desktop.
Save kariudo/6cb59eac1e1344c053c7fc4d3d919c5e to your computer and use it in GitHub Desktop.
// Converts nested objects and arrays into camelCase, when they were PascalCase to start with
var convert2Camel = function(obj) {
if (Object.prototype.toString.call(obj) === '[object Array]') {
var a = [];
obj.forEach(function(element) {
a.push(convert2Camel(element));
});
return a;
} else {
if (typeof obj == 'object')
Object.keys(obj).forEach(function(key) {
//if the key length is greater than 2 letters, lowercase the first, otherwise lowercase it all
var k = key.length > 2 ? key[0].toLowerCase() + key.substring(1) : key.toLowerCase();
if (k != key) {
var c = obj[key];
obj[k] = c;
delete obj[key];
if (c !== null && c !== undefined && typeof c == 'object') {
convert2Camel(c);
}
}
});
return obj;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment