Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const myArray = [
- {id: 1, name: 'Abhinav', email: 'abhinav@gmail.com'},
- {id: 2, name: 'Aviral', email: 'aviral@gmail.com'},
- {id: 3, name: 'Jitendra', email: 'jitendra@gmail.com'},
- {id: 4, name: 'Rajesh', email: 'rajesh@gmail.com'},
- {id: 5, name: 'Abhinav', email: 'abhinav.nigam@gmail.com'},
- {id: 6, name: 'Akash', email: 'akash@gmail.com'},
- ];
- let arrayCopy = JSON.parse(JSON.stringify(myArray));
- function checkDuplicates(propertyName, inputArray) {
- let duplicateFound = false,
- testObject = {};
- inputArray.map(function(item) {
- var itemPropertyName = item[propertyName];
- if (itemPropertyName in testObject) {
- testObject[itemPropertyName].duplicate = true;
- item.duplicate = true;
- duplicateFound = true;
- }
- else {
- testObject[itemPropertyName] = item;
- delete item.duplicate;
- }
- });
- return duplicateFound;
- }
- console.log('The Array before processing: ', myArray);
- console.log('Duplicates found in the array: '+ checkDuplicates('name', arrayCopy));
- console.log('The Array after processing: ', arrayCopy );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement