简体   繁体   中英

Get unique values from an array of objects based on two keys

I have the following array of objects:

[{x: "1", y: "2", test: "9.000", class: "low-latency"},
{x: "2", y: "3", test: "9.000", class: "low-latency"},
{x: "22", y: "22", test: "0.000", class: "low-latency"},
{x: "22", y: "22", test: "127.000", class: "high-latency"},
{x: "5", y: "6", test: "96.000", class: "high-latency"},
{x: "6", y: "7", test: "66.000", class: "low-latency"},
{x: "7", y: "8", test: "71.000", class: "high-latency"}]

This is a part of a react reducer so this gets updated every x number of seconds based on the data being returned from a socket.

What I need to do is make sure that when x and y have the same value - there should be just one of them. What is the best way to achieve this?

最简单的方法是使用_.uniqBy和哈希,例如。

_.uniqBy(arr, ({x,y}) => x+':'+y)

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