从数组中删除重复的对象,不包括JavaScript

时间:2016-07-31 13:38:22

标签: javascript arrays object

我有这个数组:

[{name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'}, {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}]

我想检查列表中的项目是否被特定键复制。

例如

- 如果列表中的名称,年龄相同,则行重复,而不关注isOlder或者更小的兄弟。

我尝试使用lodash _uniq,但没有选项可以排除键/关注特定键

7 个答案:

答案 0 :(得分:3)

你可以使用它适合你的lodash _.uniqBy(arr,'key'),因为你已经在使用lodash

答案 1 :(得分:3)

您可以使用.filter().some()

var list = [
  {name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'},
  {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}
];

list = list.filter(function(outer, oPos) {
  return !list.some(function(inner, iPos) {
    return iPos > oPos && inner.name == outer.name && inner.age == outer.age;
  });
});

console.log(list);

但是,直接在oPos + 1启动内部循环会更有效:

var list = [
  {name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'},
  {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}
];

list = list.filter(function(outer, oPos) {
  for(var iPos = oPos + 1; iPos < list.length; iPos++) {
    if(list[iPos].name == outer.name && list[iPos].age == outer.age) {
      return false;
    }
  }
  return true;
});

console.log(list);

或者,我们可以在.filter()中迭代时构建遇到的复合键的列表。大多数浏览器应该在哈希表中实现if(keyList[key])作为查找,这使得它非常快。

var list = [
  {name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'},
  {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}
];

var keyList = {};

list = list.filter(function(obj) {
  var key = obj.name + '|' + obj.age;
  
  if(keyList[key]) {
    return false;
  }
  keyList[key] = true;
  return true;
});

console.log(list);

答案 2 :(得分:2)

请试试这个

function isDuplicate(ArrayVar) {
    var tempObj = {};
    for (var key in ArrayVar) {
        if (object.hasProperty(ArrayVar[key].name)) {
            return true;
        }
        obj[ArrayVar[key].name] = true;
    }
    return false;
}

答案 3 :(得分:2)

以下是执行此操作的一个选项:

arr = [
    {name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'}, 
    {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}
    ];

var result = checkDuplicateKeys(arr[0], arr[1], ['name', 'age']);
console.log(result);

function checkDuplicateKeys(obj1, obj2, keyList) {
    var propertyDuplicateCounter = 0;
    var result = false;

    for (var i=0; i<keyList.length; i++) {
        if (obj1.hasOwnProperty(keyList[i]) && obj2.hasOwnProperty(keyList[i])) {
            propertyDuplicateCounter += 1;
        }
    }

    if (propertyDuplicateCounter == keyList.length) {
        result = true;
    }

    return result;
}

答案 4 :(得分:2)

您可以使用Array#some,如果name - age对于任何两个或更多对象相同,则会返回true其他false

var ar = [{name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'}, {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}];

var result = ar.some(function(e) {
  if(!this[e.name + '|' + e.age]) {
    this[e.name + '|' + e.age] = true;
  } else {
    return true;
  }
}, {});

console.log(result)

答案 5 :(得分:2)

尝试uniqBy +回调以从数组项中提取复合键:

uniqs = _.uniqBy(yourArray, x => x['name'] + x['age'])

纯粹的JS:

uniqBy = (xs, key) => {
    var keys = xs.map(key);
    return xs.filter((x, i) => keys.indexOf(key(x)) === i);
};

uniqs = uniqBy(yourArray, x => x['name'] + x['age'])

有关详细信息,请参阅https://stackoverflow.com/a/9229821/989121

答案 6 :(得分:1)

您可以使用Array.prototype.filter

&#13;
&#13;
var data = [
  {name: 'Brad', age: 30, isOlder: true, youngerBrother: 'Oleg'}, 
 {name: 'Brad', age: 30, isOlder: false, youngerBrother: 'Michael'}
];

function distinctByField(arr, field) {
  var result = arr.filter(function(item) {
    var key = item[field];
    return this[key] ? false : (this[key] = true);
  }, Object.create(null));
  return result;
}

console.log(distinctByField(data, 'age'));
&#13;
&#13;
&#13;