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

173
Views
how filter array of objects with same name and status in JS

Hi all I have following code

    [
      {service: { name: "Home" },status: "Generated", feeCharged:{id:1,fee:2},
      {service: { name: " Photography" },status: "requested",feeCharged:{id:2,fee:24}},
      {service: { name: "Electrical" },status: "requested",feeCharged:{id:6,fee:42}},
      {service: { name: "Electrical" },status: "Completed",feeCharged:{id:66,fee:23} },
      {service: { name: " Photography" },status: "Completed",feeCharged:{id:78,fee:11}},
      {service: { name: "Home" },status: "Generated",feeCharged:{id:45,fee:234}},
      {service: { name: "Electrical" }, status: "requested",feeCharged:{id:77,fee:3} },
      {service: { name: "Electrical" }, status: "requested",feeCharged:{id:4,fee:33} }
    ]

Now I am trying to filter them in new array by service.name and by status and add count.

The final result should be:

    [
      { name: "Home", status: "Generated", count: 2 },
      { name: "Photography", status: "requested", count: 1 },
      { name: "Photography", status: "Completed", count: 1 },
      { name: "Electrical", status: "requested", count: 3 },
      { name: "Electrical", status: "Completed", count: 1 }
    ]

So as you see in my final result if there are same service.name and status I am merging them into one object and setting count.

For filtering I try this, but it not working as I expected:

   let answer = [];

    data.forEach((x) => {
    if (!answer.some((y) => y.name === x.service.name)) {
      let newAnswer = {};
      newAnswer.name = x.service.name;
      newAnswer.value = 1;
      answer.push(newAnswer);
    } else {
      let existAnswer = answer.find((y) => y.name === x.service.name);
      existAnswer.value++;
    }
    });

Please help me to achieve that result, thanks.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Maybe this will be helpful for you?

const data = [{ service: { name: "Home" }, status: "Generated", feeCharged: { id: 1, fee: 2 }},{ service: { name: " Photography" }, status: "requested", feeCharged: { id: 2, fee: 24 } }, { service: { name: "Electrical" }, status: "requested", feeCharged: { id: 6, fee: 42 } }, { service: { name: "Electrical" }, status: "Completed", feeCharged: { id: 66, fee: 23 } }, { service: { name: " Photography" }, status: "Completed", feeCharged: { id: 78, fee: 11 } }, { service: { name: "Home" }, status: "Generated", feeCharged: { id: 45, fee: 234 } }, { service: { name: "Electrical" }, status: "requested", feeCharged: { id: 77, fee: 3 } }, { service: { name: "Electrical" }, status: "requested", feeCharged: { id: 4, fee: 33 } }];


const res = Object.entries(data.reduce((a, e, nam) => { // get counts of name/status combinations
  nam = e.service.name + "|" + e.status;
  a[nam] = (a[nam] ?? 0) + 1;
  return a;
}, {})).map(([k, n], t) =>{  // separate name/status key values again ... 
  t = k.split("|");
  return {name: t[0],status: t[1],count: n};
})

console.log(res)

(The current implementation uses | as a delimiter between the name and status value. Therefore it will fail if a name or status value contains this character.)

about 4 years ago · Santiago Trujillo 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!