Javascript 基于两个键从对象数组中获取唯一值
我有以下对象数组:Javascript 基于两个键从对象数组中获取唯一值,javascript,arrays,object,ecmascript-6,lodash,Javascript,Arrays,Object,Ecmascript 6,Lodash,我有以下对象数组: [{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:
[{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(arr, ({x,y}) => x+':'+y)
更新列表时不能执行此操作吗?这将是最有效的方法。在事实发生后再做就不那么理想了。另外,我假设您实际上需要对原始数组进行变异,而不是创建一个新的过滤数组,因为它似乎是共享的。是吗?是的,没错。。。