json - change structure of an object javascript -
i want change structure of object javascript, example:
i have structure :
obj = { "email": "abc@site.com", "societe.name": "xyz" }
and want change :
obj = { "email": "abc@site.com", "societe": { "name": "xyz" } }
thank's help.
try this:
var obj = { "email": "abc@site.com", "societe.name": "xyz" }; var newobj = {}; var keys = object.keys(obj); (var = 0; < keys.length ; i++) { var key = keys[i]; // can change '.name' if want specific if (key.indexof('.') > -1) { var splitted = key.split('.'); var innerobj = {}; innerobj[splitted[1]] = obj[key]; newobj[splitted[0]] = innerobj; } else { newobj[key] = obj[key]; } } console.log(newobj);
Comments
Post a Comment