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

221
Views
javascript group a nested array of objects by child object value

Im trying to group an array of objects by a value of one of the child objects.

Im kinda getting want I want using reduce, but it seems to be combining the group by value where the parent objects are common.

let name = [
  {
    issue: "89",
    status: ["test", "prod", "dev"]
  },
  {
    issue: "45",
    status: ["dev"]
  }
];

const groups = name.reduce((groups, item) => {
  const group = groups[item.status] || [];
  group.push(item);
  groups[item.status] = group;
  return groups;
}, {});

console.log(JSON. stringify(groups));

I get the below results

{
   "test,prod,dev":[
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ],
   "dev":[
      {
         "issue":"45",
         "status":[
            "dev"
         ]
      }
   ]
}

What id like is for it the produce the below:

{
   "prod":[
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ],
   "test":[
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ],
   "dev":[
      {
         "issue":"45",
         "status":[
            "dev"
         ]
      },
      {
         "issue":"89",
         "status":[
            "test",
            "prod",
            "dev"
         ]
      }
   ]
}

Im not sure how to produce my desired results easily.

many thanks

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

const group = Object.assign({}, ...name.reduce((p, c) => [...p, ...c.status], [])
.filter((v, i , a) => a.indexOf(v) === i)
.map(item => ({ [item]: name.filter(old => old.status.includes(item)) })))
about 4 years ago · Santiago Trujillo Report

0

You need to group your object based on the status. You can use array#reduce with array#forEach.

const names = [ { issue: "89", status: ["test", "prod", "dev"] }, { issue: "45", status: ["dev"] } ],
      result = names.reduce((r, o) => {
        o.status.forEach(name => {
          r[name] ??= [];
          r[name].push(JSON.parse(JSON.stringify(o)));
        });
        return r;
      },{});
console.log(result);

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!