0
var values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

Duplicate object will be: { name: 'Car',Data:'Required',value:'Vehicle' }

0

3 Answers 3

1

You can check if an array contains the same item on a different index and by that, understand that there is a duplicate:

const values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

const duplicates = values.filter((item, idx) => values.findIndex(i => item.name === i.name && item.Data === i.Data && item.value === i.value) !== idx);

console.log(duplicates);

Sign up to request clarification or add additional context in comments.

5 Comments

its returning empty even if duplicate row exists
@Npsad - Running my code snippet shows it works. Paste your array here..
const array = <FormArray>this.chatForm.controls['conversation']; let temp=[]; temp.push(array.value) const duplicates = temp.filter((item, idx) => temp.findIndex(i => item.createdBranch === i.createdBranch && item.responseType === i.responseType && item.query === i.query) !== idx); console.log(duplicates); dynamic values from form am getting and pushing to temp
That is not an array.
Yeah i got it !!! this solution Works Fine a small little tweak from my codebase was needed thanks!!!!
1

If you want to check all the properties of objects, you have to introduce a new function, and use it to filter your array:

var values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Different VL'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

Object.compare = function (obj1, obj2) {
	//Loop through properties in object 1
	for (var p in obj1) {
		//Check property exists on both objects
		if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;
 
		switch (typeof (obj1[p])) {
			//Deep compare objects
			case 'object':
				if (!Object.compare(obj1[p], obj2[p])) return false;
				break;
			//Compare function code
			case 'function':
				if (typeof (obj2[p]) == 'undefined' || (p != 'compare' && obj1[p].toString() != obj2[p].toString())) return false;
				break;
			//Compare values
			default:
				if (obj1[p] != obj2[p]) return false;
		}
	}
 
	//Check object 2 for any extra properties
	for (var p in obj2) {
		if (typeof (obj1[p]) == 'undefined') return false;
	}
	return true;
};


var doubles = values.filter((x,i,a) =>a.slice(0,i).find(y=>Object.compare(x,y)))

console.log(doubles)

5 Comments

This won't work. Check this fiddle. You're only ever checking if name is duplicate, whereas the OP specifically asked for a solution pointing to objects whose all values are duplicates (i.e. same as DISTINCT in SQL). Perhaps the question is a bit poorly worded.
OK, I changed my code. Please reconsider your downvote.
Nice! :) I did revoke it, but I really need to hit it, right now (4:15 AM) - so I can't validate your code. Maybe I'll check it out later.
OK I hope you won't forget it ;)
Oh! Tested it again - I love what you did there! Nice recursion. Earned an upvote. ;)
0

Working ahead from the answer in: https://stackoverflow.com/a/840808/11057988

var findDuplicates = (arr) => {
  let sorted_arr = arr.slice().sort(sortFunc); // You can define the comparing function here. 
  // JS by default uses a crappy string compare.
  // (we use slice to clone the array so the
  // original array won't be modified)
  let results = [];
  for (let i = 0; i < sorted_arr.length - 1; i++) {
    if (sortFunc(sorted_arr[i + 1], sorted_arr[i]) === 0) {
      results.push(sorted_arr[i]);
    }
  }
  return results;
}

var values = [
    { name: 'Car',Data:'Required',value:'Vehicle' },
    { name: 'Auto',Data:'Required',value:'Vehicle'},
    { name: 'Bike',Data:'Required',value:'Vehicle'},
    { name: 'Car',Data:'Required',value:'Vehicle' }
];

var sortFunc = (a, b) => {
  return   (a.name !== b.name) ? a.name.localeCompare(b.name)
         : (a.Data !== b.Data) ? a.Data.localeCompare(b.Data)
         : a.value.localeCompare(b.value);
};

console.log(`The duplicates are ${JSON.stringify(findDuplicates(values))}`);
// prints The duplicates are [{"name":"Car","Data":"Required","value":"Vehicle"}]

Couple of differences from original answer:
1. The sortFunc is defined as per your data.
2. Since your input is array of objects, the sortFunc is reused for equality check.

2 Comments

its returning empty even if duplicate row exists
If you run the code snippet in the browser, it prints the result with duplicate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.