I have some example data that shows some data related to docs (docs.id) and the people which it refers to (details.id) :
const docs = [
{
id: "89",
state: "accepted",
details: [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
}
]
},
{
id: "45",
state: "accepted",
details: [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
},
{
id: 20657,
type: "Fellow",
name: "Fellow First Name Fellow Last Name",
first_name: "Fellow First Name",
last_name: "Fellow Last Name",
type_label: "fellow"
}
]
},
{
id: "47",
state: "rejected",
details: [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
}
]
}
]
const groups = docs.reduce((groups, item) => {
const group = groups[item.details] || [];
group.push(item);
groups[item.details] = group;
return groups;
}, {});
console.log("groups: ", groups);
I'm trying to manipulate this array so that I could group per person (details.id) all her related docs (docs.id) so that I can later on use the results in react app but it's not working like that.
EDIT (adding expected result):
const new = [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
docs: [89,45,47]
},
{
id: 20656,
type: "Fellow",
name: "Fellow First Name Fellow Last Name",
docs: [47]
}
]
groups[item.details] = ...-item.detailsis itself an object, so it makes little sense to try and use that as key.item.detailsand addidas the key togroupsaccumulator