简体   繁体   English

通过 object.property 将数组过滤为唯一对象

[英]Filter array to unique objects by object.property

What I am trying to achieve is filtering of the objects array so that I get an array of objects with unique actors.我想要实现的是过滤objects数组,以便获得具有独特演员的对象数组。

So this is what I currently have:所以这就是我目前拥有的:

 var objects = [{ actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec12345", name: "name2", openid: null }, capture: 'value2' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value3' } ]; objects.filter((value, index, self) => { return self.indexOf(value) === index; }).map(ele => { console.log(ele.capture); });

The desired outcome is an array which does not consist of the last array element as this actor property matches the first array element.期望的结果是一个不包含最后一个数组元素的数组,因为此 actor 属性与第一个数组元素匹配。

But as you can see, at the moment it does not filter any of the array elements.但正如您所看到的,目前它没有过滤任何数组元素。

At first I thought that return self.indexOf(value.value) === index;起初我以为return self.indexOf(value.value) === index; would solve this issue, however this returns an empty array.可以解决这个问题,但是这会返回一个空数组。

The expected result is:预期结果是:

[{
    actor: {
      account: null,
      degraded: false,
      mbox: null,
      mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234",
      name: "name",
      openid: null
    },
    capture: 'value'
  },
  {
    actor: {
      account: null,
      degraded: false,
      mbox: null,
      mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec12345",
      name: "name2",
      openid: null
    },
    capture: 'value2'
  }
];

The objects in your array are all different objects, even if some happen to have properties with the same values.数组中的对象都是不同的对象,即使有些对象碰巧具有相同值的属性。 The .indexOf() function is comparing references, not property values. .indexOf()函数正在比较引用,而不是属性值。

Also, in practice, none of the three objects have identical properties because they all have a different .capture value.此外,在实践中,三个对象都没有相同的属性,因为它们都有不同的.capture值。

Use .findIndex() instead of .indexOf() , so that you can compare the properties to find matching objects:使用.findIndex()而不是.indexOf() ,以便您可以比较属性以找到匹配的对象:

objects.filter((value, index, self) => {
  return self.findIndex(v => v.actor.name === value.actor.name) === index;
})

Here I'm just using the .actor.name property, but of course you could compare additional properties if needed.在这里,我只是使用了.actor.name属性,当然,如果需要,您可以比较其他属性。

(Note that .findIndex() is an ES6 function, but I assume that's fine given that your code is already using arrow functions. Or you can polyfill it .) (请注意.findIndex()是一个 ES6 函数,但我认为这很好,因为您的代码已经在使用箭头函数。或者您可以对其进行polyfill 。)

 var objects = [{ actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec12345", name: "name2", openid: null }, capture: 'value2' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value3' } ]; objects.filter((value, index, self) => { return self.findIndex(v => v.actor.name === value.actor.name) === index; }).map(ele => { console.log(ele.capture); });

If you can use Object Literal to filter the unique element as well.如果您也可以使用Object Literal来过滤唯一元素。

 var objects = [{ actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value' }, { actor: { account: null, degraded: false, mbox: null, mbox_sha1sum: "843c56da78f9eb888274d2d4e12ab1d748ec46234", name: "name", openid: null }, capture: 'value3' } ]; const uniqueObjMap = {}; for (const object of objects) { uniqueObjMap[object.actor.name] = object; } const uniqueObjects = Object.values(uniqueObjMap); console.log(uniqueObjects)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM