Hey guys, I'm having an issue with a reduce function.

 Here's the function:
 
const { active, inProgress, upcoming } = momentBriefs.reduce(
(obj, brief, index) => {
console.log('index', index, 'brief', brief, obj);
if (brief.status === 'COMPLETE' && brief.startDate.isSame(todayMoment, 'week')) {
obj.active.push(brief);
} else if (brief.status === 'COMPLETE' && !brief.startDate.isSame(todayMoment, 'week')) {
obj.upcoming.push(brief);
} else if (brief.status === 'PUBLISHED') {
obj.inProgress.push(brief);
}
},
{
active: [],
inProgress: [],
upcoming: [],
},
);

 
on second and further iterations the accumulator is undefined
Anyone know the reason why?
You already invited:

Tim Callahan

Upvotes from:

You need to return what you want the obj to be for the next iteration
Just add a return obj at the end of the if else chain
also, on an unrelated note, I thought this was talking about underwear initially hehe

If you wanna answer this question please Login or Register