Skip to content

Instantly share code, notes, and snippets.

@brandonpapworth
Created June 23, 2020 13:38
Show Gist options
  • Save brandonpapworth/30250d16ba144bffd3e85f113f248a77 to your computer and use it in GitHub Desktop.
Save brandonpapworth/30250d16ba144bffd3e85f113f248a77 to your computer and use it in GitHub Desktop.
const complaints = Complaints.find({status: 'pending'});
// An object with keys of <Complaint.objectId> and values of {complaint<Complaint>, count<Number>}
// Will dedupe complaints as well as track the number of times they appear.
const complaintStore = complaints.reduce((complaintStore, complaint) => {
if (complaintStore[complaint.objectId]) {
// Already encountered this complaint, so just increment the counter
complaintStore[complaint.objectId].count += 1;
} else {
// First time seeing this complaint, so initialize the container and store it
complaintStore[complaint.objectId] = {
complaint,
count: 1,
};
}
return complaintStore;
}, {});
// An array of objects like {complaint<Complaint>, count<Number>}
const finalArray = Object.values(complaintStore);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment