-1

This is my example data. I want to group the data by month and count which month has the most data and return 1 data per month which is the most appliancesType and count.

 appliances:[
     {
        "appliancesType": "Oven",
        "createdAt": "2022-09-06"
    }, {
        "appliancesType": "Oven",
        "createdAt": "2022-11-27"
    },{
        "appliancesType": "Television",
        "createdAt": "2022-07-03"
    },{
        "appliancesType": "Television",
        "createdAt": "2022-07-03"
    }, {
        "appliancesType": "Oven",
        "createdAt": "2022-09-26"
    }];

I expecting the result like this.

appliances:[
{
 "appliancesType": "Television",
 "createdAt": "2022-07-03",
  "count": 2
},
{
 "appliancesType": "Oven",
 "createdAt": "2022-09-26",
  "count": 2
},
{
 "appliancesType": "Oven",
 "createdAt": "2022-11-27",
  "count": 1
},
]

So, how to solve this kind of problem in JavaScript?

Megane
  • 1
  • 4
  • Reopened as neither duplicate seemed close enough. The [sorting one](https://stackoverflow.com/q/979256) matched the title but not the actual question. The [grouping one](https://stackoverflow.com/q/44387647) is relatively close, but uses a simple key, not the compound one we need here. – Scott Sauyet Mar 04 '23 at 15:03

1 Answers1

0
const results = {};

data.appliances.forEach(appliance => {
  const monthYear = appliance.createdAt.substr(0, 7);
  results[monthYear] = results[monthYear] || {};
  results[monthYear][appliance.appliancesType] = (results[monthYear][appliance.appliancesType] || 0) + 1;
});

const finalResults = [];

for (const monthYear in results) {
  const appliancesForMonth = data.appliances.filter(appliance => appliance.createdAt.startsWith(monthYear));
  const highestAppliancesType = Object.keys(results[monthYear]).reduce((a, b) => results[monthYear][a] > results[monthYear][b] ? a : b);
  const resultForMonth = appliancesForMonth.map(appliance => ({ ...appliance, count: results[monthYear][appliance.appliancesType], highestAppliancesType }));
  finalResults.push(...resultForMonth);
}

console.log(finalResults)


2. Updated answers


 const results = {};

data.appliances.forEach(appliance => {
  const monthYear = appliance.createdAt.substr(0, 7);
  results[monthYear] = results[monthYear] || {};
  results[monthYear][appliance.appliancesType] = (results[monthYear][appliance.appliancesType] || 0) + 1;
});

const finalResults = [];

for (const monthYear in results) {
  const appliancesForMonth = data.appliances.filter(appliance => appliance.createdAt.startsWith(monthYear));
  let maxCount = 0;
  let maxAppliancesType = null;
  for (const appliancesType in results[monthYear]) {
    const count = results[monthYear][appliancesType];
    if (count > maxCount) {
      maxCount = count;
      maxAppliancesType = appliancesType;
    }
  }
  const resultForMonth = appliancesForMonth.find(appliance => appliance.appliancesType === maxAppliancesType);
  if (resultForMonth) {
    resultForMonth.count = maxCount;
    finalResults.push(resultForMonth);
  }
}

console.log(finalResults);
Akash
  • 132
  • 4