I want to reduce an array of objects like this:
var data = [{
item: "Item 1",
pages: 30,
chapters: 3
},
{
item: "Item 1",
pages: 50,
chapters: 3
},
{
item: "Item 2",
pages: 10,
chapters: 3
}
]
But aggregating two "fields", so in this case it should end like this:
[
{item: "Item 1", pages: 80, chapters: 6},
{item: "Item 2", pages: 10, chapters: 3}
]
I’ve tried with a reduce but I can’t aggregate more than one field:
data.reduce((acc,cur) => {
acc[cur.item][pages] = acc[cur.item][pages] + cur.item.pages || cur.item.pages
acc[cur.item][chapters] = acc[cur.item][chapters] + cur.item.chapters || cur.item.chapters
return acc
},{})
But this throws errors since it doesn’t find the pages
item in the accumulator.
Is it possible to reduce aggregating more than one field of data?
2
Answers
You can achieve this by using
reduce
and properly initializing the accumulator: