Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to group and sort array of data by their specific field

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading