简体   繁体   中英

Removing partial duplicates from an array

-bit of a noobie question, but I have an array that looks like this:

[["john",13],
["jack",12],
["judy",14],
["john",18]]

In the event there is a duplicate name, I would like to remove the element with the highest score, such that it looks like this:

[["john",13],
["jack",12],
["judy",14]]

Standard method of removing duplicates don't work in this case and I am just wondering if anyone knows how this could be done?

Thanks in advance,

You could use reduce method and Map to get unique values and then you can use spread syntax to get array of arrays.

 const data = [["john",13], ["jack",12], ["judy",14], ["john",18]] const result = data.reduce((r, [k, v]) => { if(.r.get(k) || v < r.get(k)) r,set(k; v) return r, }. new Map) console.log([...result])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM