Trying to work out how to retrieve a maximum amount of records from an array of objects
Lets say I have an array of objects like so (i've reduced the amount for readability, but there can be hundreds)
[
{ name: 'Roger East',
kick_off: 2021-10-01T14:00:00.000Z
},
{ name: 'Anthony Taylor',
kick_off: 2021-10-01T14:00:00.000Z
},
{ name: 'Mike Dean',
kick_off: 2021-09-20T14:00:00.000Z
},
{ name: 'Roger East',
kick_off: 2021-09-19T14:00:00.000Z
},
{ name: 'Mike Dean',
kick_off: 2021-08-10T14:00:00.000Z
},
{ name: 'Anthony Taylor',
kick_off: 2021-08-09T14:00:00.000Z
}
]
For my example I may only want to retrieve 1, 2, 3 or 4 records of each unique name (ordered by latest kick off date, though they are in kick_off order in the array so there may be no need to have that logic)
So lets say in my original object I have 100 records for Roger East, 90 Records for Mike Dean and 50 Records for Anthony Taylor. I would like to return the first 3 records for each name in the array (hope that makes sense)
So i would end up with
[
{ name: 'Roger East',
kick_off: 2021-10-01T14:00:00.000Z
},
{ name: 'Roger East',
kick_off: 2021-10-01T14:00:00.000Z
},
{ name: 'Roger East',
kick_off: 2021-09-20T14:00:00.000Z
},
{ name: 'Mike Dean',
kick_off: 2021-09-19T14:00:00.000Z
},
{ name: 'Mike Dean',
kick_off: 2021-08-10T14:00:00.000Z
},
{ name: 'Mike Dean',
kick_off: 2021-08-10T14:00:00.000Z
},
{ name: 'Anthony Taylor',
kick_off: 2021-08-09T14:00:00.000Z
},
{ name: 'Anthony Taylor',
kick_off: 2021-08-09T14:00:00.000Z
},
{ name: 'Anthony Taylor',
kick_off: 2021-08-09T14:00:00.000Z
}
]
Thanks