Created
May 4, 2017 20:03
-
-
Save kariudo/6cb59eac1e1344c053c7fc4d3d919c5e to your computer and use it in GitHub Desktop.
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
// 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