Skip to content

Instantly share code, notes, and snippets.

@legalt
Last active April 18, 2016 13:26
Show Gist options
  • Save legalt/e7fb4d8e839b835399e9 to your computer and use it in GitHub Desktop.
Save legalt/e7fb4d8e839b835399e9 to your computer and use it in GitHub Desktop.
Change all object keys to camelCase
var a = {
id:1,
user_name:"Vasily",
last_name:'Kratij',
law_types:{
law_type_id:0,
name:'All types'
}
};
function changeKeys(a){
for(var i in a){
var newIndex = _.camelCase(i);
a[newIndex] = a[i];
if (newIndex !== i){
delete a[i];
}
}
if (typeof a[newIndex] === 'object'){
changeKeys(a[newIndex]);
}
return a;
}
console.log(a)
console.log(changeKeys(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment