Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于两个键从对象数组中获取唯一值_Javascript_Arrays_Object_Ecmascript 6_Lodash - Fatal编程技术网

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)

更新列表时不能执行此操作吗?这将是最有效的方法。在事实发生后再做就不那么理想了。另外,我假设您实际上需要对原始数组进行变异,而不是创建一个新的过滤数组,因为它似乎是共享的。是吗?是的,没错。。。