Kelly b
Kelly b

Reputation: 211

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

Upvotes: 0

Views: 1036

Answers (2)

Nguyen Thanh
Nguyen Thanh

Reputation: 135

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)) })))

Upvotes: 1

Hassan Imam
Hassan Imam

Reputation: 22564

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);

Upvotes: 1

Related Questions