I've got an array of objects:
let recipe = [
{
Type:"AAAA",
Total: 20
},
{
Type:"BBBB",
Total: 20,
},
{
Type:"BBBB",
Total: 20,
},
{
Type:"AAAA",
Total: 20,
},
{
Type:"AAAA",
Total: 20,
}
]
And I want to get, using javascript (no libraries), the final form like this:
let finalRecipe = [["AAAA",60],["BBBB",40]]
I tried to used this but not working
let recipeModified = recipe.reduce((r, timeoff) => {
const { Type, Total } = timeoff;
const totalSum = Object.values({
Total,
}).reduce((acc, val) => ([...acc, ...val], []));
r[Type] = [...(r[Type] || []), totalSum];
return r;
}, {})
https://codesandbox.io/s/cranky-ishizaka-i7m6v?file=/src/index.js