简体   繁体   English

从数组中删除部分重复项

[英]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.您可以使用reduce方法和Map来获取唯一值,然后您可以使用扩展语法来获取 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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