I have an array of sequences, what i am trying to achieve is whichever last object completed property is true, then the next to next object will have is_to_happen as true
input
const sequences = [
{
"title": "Order placed",
"completed": true
},
{
"title": "To be confirmed",
"completed": false
},
{
"title": "Approx Thursday product will be shipped",
"completed": false
}
]
And this is what i want to have as an expected output
const output = [
{
"title": "Order placed",
"completed": true,
"is_to_happen": false
},
{
"title": "To be confirmed",
"completed": false,
"is_to_happen": false
},
{
"title": "Approx Thursday product will be shipped",
"completed": false,
"is_to_happen": true
}
]
What i have tried so far using array.reduce is not working
sequences.reduce((acc,curr) => {
acc = [...acc, curr]
if(curr.completed){
acc = [...acc, {...curr, is_to_happen: true}]
}
return acc
}, [])