this is my array of object.
const arr = [{"company":"Google","product":"A","sell":34},{"company":"Google","product":"B","sell":31},{"company":"Google","product":"C","sell":64},{"company":"Twitter","product":"A","sell":34},{"company":"Twitter","product":"B","sell":56},{"company":"Twitter","product":"C","sell":48}]
solution which i have tried.
const result = arr.reduce((acc, d) => {
const found = acc.find(a => a.name === d.name);
if (!found) {
acc.push({ name: d.name, [d.value]: d.count })
}
else {
found.push({ [d.value]: d.count });
}
return acc;
}, []);
console.log(result)
expected output should be like below but something wrong in else block
[{ company: "Google", A: 34, B:31, C:64 },{ company: "Twitter", A: 34, B:56, C:48 }]