I have an array of objects like this
[
{ id: 1, note: "TEST", member_id: 1, updated_at: "2021-04-08T15:59:08.210754", product_id: 1,
product: { id: 1, name: "Testing",},},
{id: 2, note: "", member_id: 1, updated_at: "2021-04-08T18:07:19.712304", product_id: 5,
product: { id: 5, name: "Insurance"}, },
{ id: 3, note: "One note", member_id: 1, updated_at: "2021-04-08T18:11:26.444726", product_id: 5,
product: { id: 5, name: "Insurance" },},
{ id: 4, note: "sadad", member_id: 1, updated_at: "2021-04-08T18:28:59.8435", product_id: 5, product:
{ id: 5, name: "Insurance" }, },
];
I have to represent it in a table where it can be grouped by product name and the number of objects that have the same product is counted as well as the last update of any of them is shown.
Try to use reduce but I could not advance much because I did not understand it and I used grouby with lodash but I could not advance more than that
I need it to stay like this
[
{product: "Insurance",
quantity: 3,
updated_at: "2021-04-08",
sales: [
{ id: 2,
note: "",
member_id: 1,
updated_at: "2021-04-08",
product_id: 5
},
{ id: 3,
note: "One note",
member_id: 1,
updated_at: "2021-04-08",
product_id: 5
},
{ id: 4,
note: "sadad",
member_id: 1,
updated_at: "2021-04-08",
product_id: 5
}
]
},
{product: "Testing",
quantity: 1,
updated_at: "2021-04-08",
sales: [
{id: 1,
note: "TEST",
member_id: 1,
updated_at: "2021-04-08",
product_id: 1,}
]},
]
I tried to use reduce but it was not what I expected and I did not understand it much either
array.reduce((acc, it) => {
acc[it.product.name] = acc[it.product.name] + 1 || 1;
return acc;
}, {});