const workflows = [{
id: 1,
workflow: 'bookeeping',
statuses: [{
status: 'Received'
},
{
status: 'Prepare'
},
{
status: 'Review'
},
{
status: 'Complete'
},
]
},
{
id: 2,
workflow: 'payroll',
statuses: [{
status: 'Received'
},
{
status: 'Scan'
},
{
status: 'Enter Data'
},
{
status: 'Review'
},
{
status: 'Complete'
},
]
},
{
id: 3,
workflow: 'tax preparation',
statuses: [{
status: 'Received'
},
{
status: 'Scan'
},
{
status: 'Prep'
},
{
status: 'Review'
},
{
status: 'Complete'
},
]
},
];
const engagements = [{
engagement: '1040',
workflow_id: 1,
status: 'Received'
},
{
engagement: '1040',
workflow_id: 1,
status: 'Received'
},
{
engagement: '1040',
workflow_id: 1,
status: 'Review'
},
{
engagement: '1040',
workflow_id: 2,
status: 'Review'
},
{
engagement: '1040',
workflow_id: 2,
status: 'Complete'
},
{
engagement: '1040',
workflow_id: 2,
status: 'Complete'
},
{
engagement: '1040',
workflow_id: 3,
status: 'Prep'
},
{
engagement: '1040',
workflow_id: 3,
status: 'Prep'
},
{
engagement: '1040',
workflow_id: 2,
status: 'Enter Data'
},
{
engagement: '1040',
workflow_id: 2,
status: 'Enter Data'
},
{
engagement: '1040',
workflow_id: 2,
status: 'Enter Data'
},
{
engagement: '1040',
workflow_id: 1,
status: 'Prepare'
},
{
engagement: '1040',
workflow_id: 1,
status: 'Prepare'
},
];
const res = workflows.map(({statuses, id}) => ({
workflow_id: id,
statuses: statuses.reduce((acc, cur) => {
const count = engagements.filter(({workflow_id, status}) => workflow_id === id && status === cur.status).length;
if(count === 0) return acc;
acc.push({status: cur.status, count});
return acc;
}, [])
}))
console.log(res);