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