Find duplicate values in objects with Javascript

I'm a little late to the party but this might help someone who's facing the same problem, as it is, I believe, an easier to understand solution:

const duplicates = [];
array.forEach((el, i) => {
  array.forEach((element, index) => {
    if (i === index) return null;
    if (element.name === el.name && element.Age === el.Age) {
      if (!duplicates.includes(el)) duplicates.push(el);
    }
  });
});
console.log("duplicates", duplicates);

Two things might be tricky to understand:

  • with forEach you can provide a second argument that will be an index. This is to make sure we don't compare the same two array entries with each other (if (i === index) return null; -> to abort the forEach)
  • !duplicates.includes ("if not duplicates includes) checks before adding an element to the duplicates array if it's already there. Since {} === {} is false in JS, this will not be a problem with "equal" objects already in the duplicates array, but will simply avoid adding the same element twice in one forEach loop

Edit: An even nicer solution would be this:

const duplicates = array
    .map((el, i) => {
        return array.find((element, index) => {
            if (i !== index && element.name === el.name && element.Age === el.Age) {
                return el
            }
        })
    })
    .filter(Boolean)
console.log("duplicates:", duplicates)

It has no side effects and all logic is in one if statement. The filter is needed to sort out undefined instances.


Try below snippet. It loops array through it's own elements (inner loop). 1st if condition checks for same element (we do not want that in output), 2nd if does the required condition match to identify any duplicate object.

var array = [
	{
		name: "Steven Smith",
		Country: "England",
		Age: 35
	},
	{
		name: "Hannah Reed",
		Country: "Scottland",
		Age: 23
	},
	{
		name: "Steven Smith",
		Country: "England",
		Age: 35
	},
	{
		name: "Robert Landley",
		Country: "England",
		Age: 84
	},
	{
		name: "Steven Smith",
		Country: "England",
		Age: 35
	},
	{
		name: "Robert Landley",
		Country: "England",
		Age: 84
	}
];

for(let obj of array){
	for(let ele of array){
		if(obj==ele)
			continue;
		if(ele.name===obj.name && ele.age===obj.age){
			console.log(obj);
			break;
		}
	}
}

Quick answer I came up now:

   
let result = [];

for(let item of array)
{
    for(let checkingItem of array)
    {
      if(array.indexOf(item) != array.indexOf(checkingItem) &&
        (item.name == checkingItem.name || item.Age == checkingItem.Age))
      {
        if(result.indexOf(checkingItem) == -1)
        {
          result.push(checkingItem);
        }

      }
   }
}

console.log(result);

You can use 2 reduce. The first one is to group the array. The second one is to include only the group with more than 1 elements.

var array = [{"name":"Steven Smith","Country":"England","Age":35},{"name":"Hannah Reed","Country":"Scottland","Age":23},{"name":"Steven Smith","Country":"England","Age":35},{"name":"Robert Landley","Country":"England","Age":84},{"name":"Steven Smith","Country":"England","Age":35},{"name":"Robert Landley","Country":"England","Age":84}]

var result = Object.values(array.reduce((c, v) => {
  let k = v.name + '-' + v.Age;
  c[k] = c[k] || [];
  c[k].push(v);
  return c;
}, {})).reduce((c, v) => v.length > 1 ? c.concat(v) : c, []);

console.log(result);