I have this which groups all docs per person id. This works fine and it's based on the answer from @MajedBadawi in this other stackoverflow question: Group by nested array key
const data = [
{
id: 222222,
document_file_name: "4020653_FileName.pdf",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
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: 1111111,
document_file_name: "4020600_FileName.pdf",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "rejected",
details: [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
}
]
},
{
id: 333333,
document_file_name: "4020890_FileName.pdf",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "rejected",
details: [
{
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: 444444,
document_file_name: "4020672_FileName.pdf",
document_updated_at: "2020-07-08T19:41:28.385Z",
document_type_label: "doc label",
state: "rejected",
details: [
{
id: 20656,
type: "Claimant",
name: "First Name Last Name",
first_name: "First Name",
last_name: "Last Name",
type_label: "claimant"
}
]
}
];
let name = [
{
id: "89",
state: "accepted",
document_type_label: "doc label",
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",
document_type_label: "doc label",
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",
document_type_label: "doc label",
details: [
{
id: 20657,
type: "Fellow",
name: "Fellow First Name Fellow Last Name",
first_name: "Fellow First Name",
last_name: "Fellow Last Name",
type_label: "fellow"
}
]
}
];
const groups = [
...data
.reduce(
(detailMap, { id: docId, document_file_name, document_type_label, state, details = [] }) => {
details.forEach(({ id, type, name }) => {
const detail = detailMap.get(id) ?? { id, type, name, docs: [] };
detail.docs.push({ docId, document_file_name, document_type_label, state });
detailMap.set(id, detail);
});
return detailMap;
},
new Map()
)
.values()
];
console.log("groups: ", groups);
The problem is that I shouldn't have "repeated" docs in individual person as when it comes, for example, to doc id: "222222". If there is a doc that has more than one person associated to it, it should show something like:
{
"id": [{"id":20656},{"id":20657}]
"name": [{"name":"First Name Last Name"},{"name": "Fellow First Name Fellow Last Name"}]
"docs": [
{
"docId": 222222,
"document_file_name": "4020653_FileName.pdf",
"document_type_label": "doc label",
"state": "accepted"
}
]
}
namearray doing?data.details.name) that relate to that doc (data.id). As of right now, it shows only one name because it repeats the same doc per person if they both relate to it