I have an array of objects that denotes running status of some processes over time.
[
{ process: 'first' , running:true}, // starting 'first'
{ process: 'second', running:true}, // starting 'second'
{ process: 'first' , running:true}, // starting 'first' again
{ process: 'second', running:false}, // stopping 'second'
{ process: 'first' , running:false} // stopping 'first' ( there is one more running which is not yet stopped)
]
I would like to group this array by process and provide the current running status. Expected output,
{
'first' : true, // started two times but stopped only once. So there one more running
'second' : false // started and stopped once
}
What I did is to group this object array into a single object like,
{
'first': [true,true,false],
'second': [true,false]
}
code,
arr.reduce((acc, item) => {
const key = item.process;
if (!acc[key]) acc[key] = [];
acc[key].push(item.running);
return acc;
}, {});
Is there any elegant way now to achieve my target from this state?