簡體   English   中英

根據兩個鍵從對象數組中獲取唯一值

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

我有以下對象數組:

[{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"}]

這是React Reducer的一部分,因此它會根據從套接字返回的數據每x秒更新一次。

我需要做的是確保x和y的值相同時-應該只是其中之一。 實現此目標的最佳方法是什么?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM