-4

I have this JSON data

myArray = [
  { 'category': 'apple', 'date': '20/4/2023', 'time1': '12:30' },
  { 'category': 'banana', 'date': '20/4/2023', 'time1': '12:32' },
  { 'category': 'tv', 'date': '21/4/2023', 'time1': '12:34' },
  { 'category': 'banana', 'date': '21/4/2023', 'time1': '12:39' },

];

I want to show this data on a HTML page as line of date and under each date all related category to that date + time1.

Expected output can be as:

----------20/4/2023-------------
apple 12:30
banana 12:32
----------21/4/2023-------------
tv 12:34
banana 12:39
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

You can do something like this:

var groupedArray = myArray.reduce((acc, curr) => {
  acc[curr.date] = acc[curr.date] || 0;
  acc[curr.date]++;
  return acc;
}, {});

for (var date in groupedArray) {
  console.log(`${date}: ${groupedArray[date]}`);
}

This will give you the output as count by date.

Si8
  • 9,141
  • 22
  • 109
  • 221