JavaScript数组不会过滤重复的项目

时间:2017-08-10 03:31:19

标签: javascript arrays filter duplicates

我的数组过滤器不起作用,我不确定哪个部分出错了。我的样本数据:

var arr = [
    {accountID: '-KqIR-HT7orcPpe-lZa8', age: 31, gender: 'female'},
    {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
    {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
    {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
    {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
    {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
    {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
    {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
    {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
    {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
    {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female}
];

我想过滤掉相同的帐户ID。我在网上找到了这个解决方案:

arr = arr.filter( function( item, index, inputArray ) {
   return inputArray.indexOf(item) == index;
});

当我尝试使用以下方法打印出已过滤的数组时:

for(var i = 0; i < arr.length; i++){
        console.log(arr[i][0].accountID + ' ' + arr[i][0].age + ' ' + arr[i][0].gender);
}

我找回完全相同的数组,结果重复。哪部分错了?谢谢!

2 个答案:

答案 0 :(得分:2)

请改用findIndex,因为这些对象都不同。

&#13;
&#13;
var arr = [{
    accountID: '-KqIR-HT7orcPpe-lZa8',
    age: 31,
    gender: 'female'
  },
  {
    accountID: '-KqIR-GvEpHFiPFZRxbG',
    age: 59,
    gender: 'female'
  },
  {
    accountID: '-KqIR-GvEpHFiPFZRxbG',
    age: 59,
    gender: 'female'
  },
  {
    accountID: '-KqIR-GvEpHFiPFZRxbG',
    age: 59,
    gender: 'female'
  },
  {
    accountID: '-KqIR-GvEpHFiPFZRxbG',
    age: 59,
    gender: 'female'
  },
  {
    accountID: '-KqIR-EKbN02zAfCRyoe',
    age: 24,
    gender: 'female'
  },
  {
    accountID: '-KqIR-EKbN02zAfCRyoe',
    age: 24,
    gender: 'female'
  },
  {
    accountID: '-KqIR-EKbN02zAfCRyoe',
    age: 24,
    gender: 'female'
  },
  {
    accountID: '-KqIR-EKbN02zAfCRyoe',
    age: 24,
    gender: 'female'
  },
  {
    accountID: '-KqIR-EKbN02zAfCRyoe',
    age: 24,
    gender: 'female'
  },
  {
    accountID: '-KqIR-EKbN02zAfCRyoe',
    age: 24,
    gender: 'female'
  }
];
arr = arr.filter(function(item, index, inputArray) {
  return inputArray.findIndex(el => el.accountID === item.accountID) == index;
});

console.log(arr);
&#13;
&#13;
&#13;

您还可以使用Map

&#13;
&#13;
var arr = [{
        accountID: '-KqIR-HT7orcPpe-lZa8',
        age: 31,
        gender: 'female'
      },
      {
        accountID: '-KqIR-GvEpHFiPFZRxbG',
        age: 59,
        gender: 'female'
      },
      {
        accountID: '-KqIR-GvEpHFiPFZRxbG',
        age: 59,
        gender: 'female'
      },
      {
        accountID: '-KqIR-GvEpHFiPFZRxbG',
        age: 59,
        gender: 'female'
      },
      {
        accountID: '-KqIR-GvEpHFiPFZRxbG',
        age: 59,
        gender: 'female'
      },
      {
        accountID: '-KqIR-EKbN02zAfCRyoe',
        age: 24,
        gender: 'female'
      },
      {
        accountID: '-KqIR-EKbN02zAfCRyoe',
        age: 24,
        gender: 'female'
      },
      {
        accountID: '-KqIR-EKbN02zAfCRyoe',
        age: 24,
        gender: 'female'
      },
      {
        accountID: '-KqIR-EKbN02zAfCRyoe',
        age: 24,
        gender: 'female'
      },
      {
        accountID: '-KqIR-EKbN02zAfCRyoe',
        age: 24,
        gender: 'female'
      },
      {
        accountID: '-KqIR-EKbN02zAfCRyoe',
        age: 24,
        gender: 'female'
      }
    ];
    let m = new Map(arr.map(obj => [obj.accountID, obj]));
    let noDuplicates = [...m.values()];
    
    console.log(noDuplicates);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在ES2015中,您可以使用 Set() 来存储任何类型的唯一值。

以下示例使用 .forEach() 循环遍历数组中的每个值,并将唯一值添加到辅助数组uniq。请注意,您需要stringifyparse数组中的对象。

&#13;
&#13;
var arr = [{accountID: '-KqIR-HT7orcPpe-lZa8', age: 31, gender: 'female'}, {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'}, {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'}, {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender:
'female'}, {accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'}, {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'}, {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'}, {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender:
'female'}, {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'}, {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'}, {accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'}];

var uniq = new Set();
arr.forEach(e => uniq.add(JSON.stringify(e)));
var res = Array.from(uniq).map(e => JSON.parse(e));
console.log(res);
&#13;
&#13;
&#13;

希望这有帮助! :)