Here's my code: It could be run in an online lodash tester. Currently, I am getting voq_count by checking the matching recall_ids and then looping through the array to attach voq_count property.
let array = [{
"recall_id": 527,
"type_cd": "E",
"odi_id": "70E001"
},
{
"recall_id": 528,
"type_cd": "T",
"odi_id": "70T002"
},
{
"recall_id": 529,
"type_cd": "T",
"odi_id": "70T003"
}
];
let dataItem = {
"results": [
{
"p_hazards": [{
"entity_record_id": 529
},
{
"entity_record_id": 528
}
],
"received_dt": "2005-08-24T00:00:00.000Z",
"odi_id": "55555825",
"complaint_id": 55555833,
"number_injured": 4,
"number_deaths": 1
},
{
"p_hazards": [{
"entity_record_id": 547
},
{
"entity_record_id": 545
},
{
"entity_record_id": 546
}
],
"received_dt": "2005-08-24T00:00:00.000Z",
"odi_id": "55555636",
"complaint_id": 55555646,
"number_injured": 0,
"number_deaths": 0
},
{
"p_hazards": [{
"entity_record_id": 528
}],
"received_dt": "2005-08-29T00:00:00.000Z",
"odi_id": "1147792",
"complaint_id": 1147792,
"number_injured": 0,
"number_deaths": 0
},
]
};
const RIds = [];
_.map(dataItem.results, (ph) => {
_.map(ph.p_hazards, (id) => {
RIds.push(id);
});
});
const getCount = _.countBy(
RIds,
'entity_record_id'
);
_.map(array, (r) => {
const voqCount = getCount[r.recall_id.toString()];
r.voq_count = voqCount || 0;
return r;
});
Current output:
[
{
"recall_id": 527,
"type_cd": "E",
"odi_id": "70E001",
"voq_count": 0
},
{
"recall_id": 528,
"type_cd": "T",
"odi_id": "70T002",
"voq_count": 2
},
{
"recall_id": 529,
"type_cd": "T",
"odi_id": "70T003",
"voq_count": 1
}
]
I am trying to loop through for the matching recall_ids and construct a voqs array which would look like:
Expected Output:
[
{
"recall_id": 527,
"type_cd": "E",
"odi_id": "70E001",
"voq_count": 0,
"voqs": []
},
{
"recall_id": 528,
"type_cd": "T",
"odi_id": "70T002",
"voq_count": 2,
"voqs": [{
"received_dt": "2005-08-24T00:00:00.000Z",
"odi_id": "55555825",
"complaint_id": 55555833,
"number_injured": 4,
"number_deaths": 1
}, {
"received_dt": "2005-08-29T00:00:00.000Z",
"odi_id": "1147792",
"complaint_id": 1147792,
"number_injured": 0,
"number_deaths": 0
}]
},
{
"recall_id": 529,
"type_cd": "T",
"odi_id": "70T003",
"voq_count": 1,
"voqs": [{
"received_dt": "2005-08-24T00:00:00.000Z",
"odi_id": "55555825",
"complaint_id": 55555833,
"number_injured": 4,
"number_deaths": 1
}
]
}
]