I have this nested object which I want to sort alphabetically,
var obj = {
'ABCD' : {
'E' : {},
'R' : {},
'A' : {},
'E' : {}
},
'WXYZ' : {
'F' : {},
'M' : {},
'G' : {},
'A' : {}
},
'MNOP' : {
'A' : {},
'H' : {},
'B' : {},
'C' : {}
},
'EFGH' : {
'D' : {},
'C' : {},
'B' : {},
'E' : {}
}
}
I want the output to be like this-
{
'ABCD' : {
'A' : {},
'E' : {},
'E' : {},
'R' : {}
},
'EFGH' : {
'B' : {},
'C' : {},
'D' : {},
'E' : {}
},
'MNOP' : {
'A' : {},
'B' : {},
'C' : {},
'H' : {}
},
'WXYZ' : {
'A' : {},
'F' : {},
'G' : {},
'M' : {}
}
}
I have written code to sort direct keys, but I am not getting how can I sort further object within those keys, Here's my code-
var sorted = {}
Object.keys(obj).sort().forEach(function(val) {
sorted[val] = obj[val]
})
Please suggest on how can I achieve the output mentioned above. Thanks.