[Solved] Best Way to “Group By” an Array of Objects in ES6 JS?


Cleanest solution I could find to do this is: Create a dictionary, filter out any records not needed, loop through the dictionary and keep the task with the next closest date:

const dict = {};
input.filter(n => n.is_completed === 0 && new Date(n.date_due) - new Date() > 0)
.forEach(n => {
    dict[n.task] = dict[n.task] || n;
    if (new Date(dict[n.task].date_due) < new Date(n.date_due)) {
      dict[n.task] = n;
    }
});
return dict;

solved Best Way to “Group By” an Array of Objects in ES6 JS?