Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Typescript/Javascript Count duplicate values based on 2 parameters

I am having a hard time manipulating my data to be useful in a bar chart.

This is the data is ended up with after mapping it with only the properties i need:

const data = [
    {
        date: "2021-10-10",
        serialNumber: "4A6211B92417A7DB"
    },
    {
        date: "2021-10-11",
        serialNumber: "5B6211B92417A7DB"
    },
    {
        date: "2021-10-12",
        serialNumber: "5B6211B92417A7DB"
    },
    {
        date: "2021-10-12",
        serialNumber: "4A6211B92417A7DB"
    },
    {
        date: "2021-10-12",
        serialNumber: "4A6211B92417A7DB"
    },
    {
        date: "2021-10-12",
        serialNumber: "4A6211B92417A7DB"
    }
];

What i want to have is the following: a count of the duplicates based on the date per serialNumber. It would look like this:

const solution = [
    {
        serialNumber: "4A6211B92417A7DB",
        count: 3,
        date: "2021-10-12",
    },
    {
        serialNumber: "5B6211B92417A7DB",
        count: 1,
        date: "2021-10-12",
    },
    {
        serialNumber: "5B6211B92417A7DB",
        count: 1,
        date: "2021-10-11",
    },
    {
        serialNumber: "5B6211B92417A7DB",
        count: 1,
        date: "2021-10-10",
    },
]

What i came up with was the following:

array.forEach(function(obj: any) {
       const key = JSON.stringify(obj.serialNumber)
       counts[key] = (counts[key] || 0) + 1
    });

but then it only count them based on 1 parameter and i also need the count per date aswel. How do you do this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a 'group-by' with a composite key formed of the date and serial number. Here using a for..of loop to accumulate into an object, creating the composite key using a template literal and then assigning the Object.values() as the result.

const data = [{ date: '2021-10-10', serialNumber: '4A6211B92417A7DB', }, { date: '2021-10-11', serialNumber: '5B6211B92417A7DB', }, { date: '2021-10-12', serialNumber: '5B6211B92417A7DB', }, { date: '2021-10-12', serialNumber: '4A6211B92417A7DB', }, { date: '2021-10-12', serialNumber: '4A6211B92417A7DB', }, { date: '2021-10-12', serialNumber: '4A6211B92417A7DB', },];

const counts = {};
for (const datum of data) {
  const k = `${datum.date}_${datum.serialNumber}`;
  // using OR short circuit
  (counts[k] || (counts[k] = { ...datum, count: 0 })).count += 1;
  
  // alternativley with logical nullish assignment if available
  //(counts[k] ??= { ...datum, count: 0 }).count += 1;
}

const result = Object.values(counts);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!