Find duplicate object in array and mark these [Javascript lodash]

Ann*_*her 5 javascript arrays object lodash

I have an array with objects:

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"
  }
];
Run Code Online (Sandbox Code Playgroud)

I would like to mark the objects that they are duplicates, like this (notice the extra property, 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
  }
];
Run Code Online (Sandbox Code Playgroud)

If one of the above values change (type, name, country, postalZone or cityName), the object is unique.

I have tried the following for testing purpose. Getting only the unique objects for "type", does not work for me e.g.

_.uniq(test, "type");
Run Code Online (Sandbox Code Playgroud)

Still getting all the array objects. Ofcourse I want to check on more object keys, not only on type, but on all object keys (type, name, country, postalZone or cityName).

The native array function "some", stops if it finds the first duplicate, for me it should not stop...

How can I achieve this?

I have Lodash v4.13.1.

Nen*_*car 8

这是一种解决方案,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)
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Run Code Online (Sandbox Code Playgroud)