How To Sort Two Nested Objects?
I have a problem sorting this nested objects; the object I have is: Array of objects: [Object, Object] inside this array there are two objects and inside those objects are 1 objec
Solution 1:
try this
var source = [{'2016-5': [{shift0: [26]},
{shift1: [42]},
{shift2: [53]},
{shift3: [31]}]},
{'2016-6': [{shift0: [33]},
{shift1: [15]},
{shift2: [13]},
{shift3: [66]}]}
];
source.forEach( function(obj){
Object.keys(obj).forEach( function(key){
console.log(obj[key]);
obj[key].sort( function(a,b){
return a[ Object.keys(a)[0] ] - b[ Object.keys(b)[0] ] ;
})
});
});
document.body.innerHTML += JSON.stringify(source, 0, 4 );
Solution 2:
The JSON you gave is invalid, assuming your JSON as below:
varsource= [{'2016-5': [{shift0: [26]},
{shift0: [42]},
{shift0: [53]},
{shift0: [31]}]},
{'2016-6': [{shift0: [33]},
{shift0: [15]},
{shift0: [13]},
{shift0: [66]}]}
];
Then you could try :
var result =
source.map(function(item){
Object.keys(item)
.map(function(key){
item[key] =
item[key].sort(function(p, c){
return p.shift0[0]-c.shift0[0];
});
});
return item;
});
Post a Comment for "How To Sort Two Nested Objects?"