按object.property将数组过滤为唯一的对象 [英] Filter array to unique objects by object.property

查看:44
本文介绍了按object.property将数组过滤为唯一的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是过滤 objects 数组,以便获得具有 unique actor的对象数组.

What I am trying to achieve is filtering of the objects array so that I get an array of objects with unique actors.

这就是我目前拥有的:

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);
});

所需结果是一个不包含最后一个数组元素的数组,因为此actor属性与第一个数组元素匹配.

The desired outcome is an array which does not consist of the last array element as this actor property matches the first array element.

但是正如您所看到的,目前它不过滤任何数组元素.

But as you can see, at the moment it does not filter any of the array elements.

起初我以为 return self.indexOf(value.value)=== index; 可以解决此问题,但是这将返回一个空数组.

At first I thought that return self.indexOf(value.value) === index; would solve this issue, however this returns an empty array.

预期结果是:

[{
    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'
  }
];

推荐答案

即使某些对象碰巧具有相同值的属性,数组中的对象也都是不同的对象. .indexOf()函数正在比较引用,而不是属性值.

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.

此外,实际上,这三个对象都不具有相同的属性,因为它们都具有不同的 .capture 值.

Also, in practice, none of the three objects have identical properties because they all have a different .capture value.

使用 .findIndex() 而不是 .indexOf(),这样您就可以比较属性以找到匹配的对象:

Use .findIndex() instead of .indexOf(), so that you can compare the properties to find matching objects:

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

这里我只是使用 .actor.name 属性,但是如果需要,您当然可以比较其他属性.

Here I'm just using the .actor.name property, but of course you could compare additional properties if needed.

(请注意, .findIndex()是ES6函数,但是鉴于您的代码已在使用箭头函数,因此我认为这很好.或者您也可以

(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.)

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);
});

这篇关于按object.property将数组过滤为唯一的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆