![](/img/trans.png)
[英]How to get objects with unique values for specific keys from array of objects?
[英]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.