Chartjs-Reduce nested object based on attribute javascript

0👍

So a basic groupBy function ?

const data = [
  { id: '1', description: 'desc 1', number: 1 },
  { id: '2', description: 'desc 2', number: 2 },
  { id: '3', description: 'desc 2', number: 3 },
  { id: '4', description: 'desc 1', number: 4 },
  { id: '5', description: 'desc 4', number: 5 },
];

const grouped = data.reduce((p, n) => ({
  ...p,
  [[n.description]]: (p[n.description] ?? []).concat(n)
}), {});

console.log(grouped);

Leave a comment