Last active
January 26, 2021 10:32
-
-
Save orcaman/dd40b04e0e42eed841d7 to your computer and use it in GitHub Desktop.
Get a lowercase representation of a JSON object, recursively (JS)
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 toLowerCase(obj) { | |
if (!obj) { | |
return; | |
} | |
if (typeof obj !== 'Object' && typeof obj !== 'object') { | |
return; | |
} | |
var keys = Object.keys(obj); | |
var result = {}; | |
keys.map(function(k, v) { | |
if (typeof k === 'string') { | |
if (typeof obj[k] === 'string') { | |
result[k.toLowerCase()] = obj[k].toLowerCase(); | |
} else { | |
// if the node is an object, perform the same process over that node | |
if (typeof obj[k] === 'Object' || typeof obj[k] === 'object') { | |
result[k.toLowerCase()] = toLowerCase(obj[k]); | |
} else { | |
result[k.toLowerCase()] = obj[k]; | |
} | |
} | |
} | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks friend..