Created
June 23, 2020 13:38
-
-
Save brandonpapworth/30250d16ba144bffd3e85f113f248a77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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