Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 如何从Angular 5中的数组中获取基于多列的唯一值?_Javascript_Arrays_Object_Lodash - Fatal编程技术网

Javascript 如何从Angular 5中的数组中获取基于多列的唯一值?

Javascript 如何从Angular 5中的数组中获取基于多列的唯一值?,javascript,arrays,object,lodash,Javascript,Arrays,Object,Lodash,我有一个类似的数组 [{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }] 其中需要独特的X&Y组合 输出为: [{ 's':1,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }] 因为第一项和第二

我有一个类似的数组

[{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }]
其中需要独特的X&Y组合

输出为:

[{ 's':1,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }]
因为第一项和第二项中的X和Y值相同。1必须移除

对于UniqBy,我使用的是单列

_.uniqBy([{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }], 'y');
但我需要两个或更多的钥匙组合

_.uniqBy([{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }], '[x]','[y]'); 
而且很多。他们都不工作/

[{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y

_.uniqBy([{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }], 'y');



[{ 's':1,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }]

使用
filter
findIndex
获得一个极其简单的普通JavaScript解决方案(在Lodash中完成的一切都可以在JavaScript中完成,Lodash只是简化了事情):

const arr=[{'s':1,'x':1,'y':1},{'s':2,'x':1,'y':1},{'s':3,'x':1,'y':2},{'s':4,'x':2,'y':1},{'s':5,'x':2,'y';
const res=arr.findIndex(({x,y},i,a)=>i==a.findIndex(e=>e.x==x&&e.y==y));
控制台日志(res)

。作为控制台包装{max height:100%!important;top:auto;}
您可以使用回调函数并连接数字

console.log(qby.uniqBy([
{'s':1,'x':1,'y':1},
{'s':2,'x':1,'y':1},
{'s':3,'x':1,'y':2},
{'s':4,'x':2,'y':1},
{s':5,'x':2,'y':2}
],el=>`${el.x}${el.y}`)

我调整了标记,因为这似乎不是一个角度相关的问题,而是关于lodash的
uniqBy
的问题,我只想知道为什么我得到的错误属性“x”在类型“String”上不存在。与uu.uniqBy