在数组中查找重复的对象并标记这些[Javascript lodash]

时间:2016-09-16 11:01:56

标签: javascript arrays object lodash

我有一个包含对象的数组:

var test = [
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "Some one else home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home1",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  }
];

我想标记它们重复的对象,如下所示(请注意额外的属性,isDuplicate):

var test = [
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "Some one else home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity",
    isDuplicate: true
  },
	{
  	type: "home1",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity",
    isDuplicate: true
  }
];

如果以上某个值发生更改(type,name,country,postalZone或cityName),则该对象是唯一的。

我已尝试以下测试目的。仅获取“类型”的唯一对象对我不起作用,例如

_.uniq(test, "type");

仍然获取所有数组对象。当然,我想检查更多的对象键,不仅是在类型上,还在所有对象键上(类型,名称,国家,邮政区域或城市名称)。

本机数组函数“some”,如果找到第一个副本就会停止,对我来说它不应该停止...

我怎样才能做到这一点?

我有Lodash v4.13.1。

2 个答案:

答案 0 :(得分:3)

以下是map()一个解决方案的解决方案,如果找到重复对象并找到重复项,我使用了find()isEqual()。因此,如果找到重复,它将添加属性,否则它将只返回对象。

var test = [{
  type: "home",
  name: "My home",
  country: "DE",
  postalZone: "1234",
  cityName: "GermanCity"
}, {
  type: "home",
  name: "Some one else home",
  country: "DE",
  postalZone: "1234",
  cityName: "GermanCity"
}, {
  type: "home",
  name: "My home",
  country: "DE",
  postalZone: "1234",
  cityName: "GermanCity"
}, {
  type: "home1",
  name: "My home",
  country: "DE",
  postalZone: "1234",
  cityName: "GermanCity"
}, {
  type: "home",
  name: "My home",
  country: "DE",
  postalZone: "1234",
  cityName: "GermanCity"
}];


var result = _.map(test, function(o, i) {
  var eq = _.find(test, function(e, ind) {
    if (i > ind) {
      return _.isEqual(e, o);
    }
  })
  if (eq) {
    o.isDuplicate = true;
    return o;
  } else {
    return o;
  }
})

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

答案 1 :(得分:2)

实际上不需要lodash。通过Object.prototype.compare()和纯JS的发明,您可以按照以下方式执行此操作;

&#13;
&#13;
Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};

var myList = [
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "Some one else home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home1",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  },
	{
  	type: "home",
    name: "My home",
    country: "DE",
    postalZone: "1234",
    cityName: "GermanCity"
  }
],
markedList = myList.reduce((p,c) => p.some(o => o.compare(c)) ? (c.isDuplicate = true, p.concat(c))
                                                              : p.concat(c),[]);
console.log(JSON.stringify(markedList,null,4));
&#13;
&#13;
&#13;

您当然可能希望将Object.prototype.compare()转换为正确的函数并相应地调整代码。