Skip to content

Instantly share code, notes, and snippets.

@mcalavera81
Created October 23, 2020 22:47
Show Gist options
  • Save mcalavera81/d1f9428999a5c2ea3fcaef6c1ca7bbca to your computer and use it in GitHub Desktop.
Save mcalavera81/d1f9428999a5c2ea3fcaef6c1ca7bbca to your computer and use it in GitHub Desktop.
function ConvertKeysToLowerCase(obj) {
if (!obj || typeof obj !== "object") {
return obj;
}
var output = {};
for (let i in obj) {
if (Object.prototype.toString.apply(obj[i]) === '[object Object]') {
output[i.toLowerCase()] = ConvertKeysToLowerCase(obj[i]);
}else if(Object.prototype.toString.apply(obj[i]) === '[object Array]'){
output[i.toLowerCase()]=[];
for (const z of obj[i]){
output[i.toLowerCase()].push(ConvertKeysToLowerCase(z));
}
} else {
output[i.toLowerCase()] = obj[i];
}
}
return output;
};
let a ={
Pepe:[{
LIBRO: "PEPE",
MANSION:{
"a":1
}
},{
LIBRO: "PEPE",
MANSION:{
"a":1
}
}],
CASA: "SI",
Perro:{
LIBRO: "PEPE",
MANSION:{
"a":1
}
}
}
console.log(JSON.stringify(ConvertKeysToLowerCase(a),null,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment