I would like to know how to change array object to nested object in javascript.
I have list as array object, how to convert to nested object
function nestedobj(arrlist){
var result ={};
result.list1 = arrlist[0];
result.list2 = arrlist[1]
return list;
}
var list= [
{id: 1, cn: "SG"},
{id: 2, cn: "TH"}
]
var list1= [
{id: 1, cn: "SG"},
{id: 2, cn: "TH"},
{id: 3, cn: "MY"}
]
var listobj = this.nestedobj(list);
var listobj1 = this.nestedobj(list1);
console.log(listobj)
console.log(listobj1)
Expected Output
{
"list1":{"id": 1, "cn": "SG"},
"list2":{"id": 2, "cn": "TH"}
}
{
"list1":{"id": 1, "cn": "SG"},
"list2":{"id": 2, "cn": "TH"},
"list3":{"id": 3, "cn": "MY"}
}
listtonestedobj()before you define it, so it's undefined