var listx = [
{
name: 'user:20',
id: '1720620993953-0'
},
{
name: 'user:30',
id: '1720621228109-0'
},
{
name: 'user:30',
id: '1720572420832-0'
},
{
name: 'user:30',
id: '1720572410558-0'
},
]
I’m trying to get the max id for each name, and the name can repeat like shown below.
So for the example, it would be a Map:
'user:20': '1720620993953-0'
'user:30': '1720621228109-0'
I tried searching everything, but didn’t get a response.
3
Answers
You can iterate through each item in the list. Then for each item, check if the
id
is greater than the already existing value and "save" it if it is.You can simply iterate over the objects and aggregate these data in a new object, conditionally setting the
id
value:Use
Array.prototype.reduce()
to run through the list and build the desired result: