Creat Array Of Json Objects From Mulitple Objects Using Linq.js
I have a JSON array like so: var jsonArray = [ { Date: '2010-02-25', Size:'Large', Type:'a', Value: '100'}, { Date: '2010-02-25', Size:'Medium', Type:'a', Value: '160'},
Solution 1:
Here's one way to accomplish this:
var jsonArray = [
{ Date: "2010-02-25", Size:"Large", Type:"a", Value: "100"},
{ Date: "2010-02-25", Size:"Medium", Type:"a", Value: "160"},
{ Date: "2010-02-25", Size:"Small", Type:"a", Value: "200"},
{ Date: "2010-02-25", Size:"Large", Type:"b", Value: "400"},
{ Date: "2010-02-25", Size:"Medium", Type:"b", Value: "120"},
{ Date: "2010-02-25", Size:"Small", Type:"b", Value: "170"}
];
functionmakeGroupKey(item) {
returnJSON.stringify({
Date: item.Date,
Type: item.Type
});
}
functionmergeItems(items) {
return items.Aggregate(JSON.parse(items.Key()), function (prev, next) {
prev[next.Size] = next.Value;
return prev;
});
}
var result = Enumerable.From(jsonArray)
.GroupBy(makeGroupKey)
.Select(mergeItems)
.ToArray();
console.log(result);
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>
Post a Comment for "Creat Array Of Json Objects From Mulitple Objects Using Linq.js"