I would like add up all the duplicate values for each day and sort them by object key
let meals = {
'Sat Jul 11 2020': [{ fruit: "apple" }, { fruit: "apple" }, { fruit: "orange" }, { fruit: "apple" }],
'Sat Jul 04 2020': [{ fruit: "orange" }, { fruit: "apple" }, { fruit: "orange" }],
'Fri Jul 03 2020': [{ fruit: "orange" }, { fruit: "orange" }, { fruit: "apple" }, { fruit: "orange" }]
}
let keys = Object.keys(meals);
let food = keys.map(item => {
return meals[item].map((x) => {
return x.fruit
});
});
var sorted ={};
food.forEach(i => {
i.map((x) => {
return [sorted[x] = (sorted[x] || 0) + 1];
})
});
What i'm after is something like this:
'Sat Jul 11 2020': [{apple: 3, oranges: 1}],
'Sat Jul 04 2020': [{apple: 1, oranges: 2}],
'Fri Jul 03 2020': [{apple: 1, oranges: 3}],
what I got at the moment is its adds up the values for all the days combined and it doesn't sort it by day
any ideas?