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