Review the JavaScript object provided below (note that only a portion of the object is shown).
https://i.sstatic.net/5H0gn.png
Here is the requirement:
For each distinct user, limit the number of random leads to a maximum of 4 and discard the rest.
For instance, if there are 10 unique users, each appearing 5 times, the object will contain 50 records. The goal is to retain only 4 records for each user, resulting in a total of 40 records.
If you're unsure how to approach this problem, below are the initial steps I have taken:
1) Form an array comprising a list of unique users:
["1116", "1075", "1124"]
2) The next steps are unclear. It seems a logical approach would be to iterate through the object (referred to as 'leads') and compare it with the unique users array. If there's a match, increment a counter. Once the counter reaches 4, the lead should be excluded. Here is a draft of the pseudo code:
for (var i = 0; i < leads.length; i++) {
if (leads[i].user == //anything in the users array//) {
// Check for an existing count property on the unique users array
// If it doesn't exist, add a count property to the users array
// Otherwise, if the count is already 4, remove [leads[i]]
}//end if
}//end for
Does this approach seem appropriate, or is there a more efficient method in JavaScript to achieve this?