Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. const myArray = [
  2. {id: 1, name: 'Abhinav', email: 'abhinav@gmail.com'},
  3. {id: 2, name: 'Aviral', email: 'aviral@gmail.com'},
  4. {id: 3, name: 'Jitendra', email: 'jitendra@gmail.com'},
  5. {id: 4, name: 'Rajesh', email: 'rajesh@gmail.com'},
  6. {id: 5, name: 'Abhinav', email: 'abhinav.nigam@gmail.com'},
  7. {id: 6, name: 'Akash', email: 'akash@gmail.com'},
  8. ];
  9. let arrayCopy = JSON.parse(JSON.stringify(myArray));
  10.  
  11.  
  12. function checkDuplicates(propertyName, inputArray) {
  13. let duplicateFound = false,
  14. testObject = {};
  15.  
  16. inputArray.map(function(item) {
  17. var itemPropertyName = item[propertyName];
  18. if (itemPropertyName in testObject) {
  19. testObject[itemPropertyName].duplicate = true;
  20. item.duplicate = true;
  21. duplicateFound = true;
  22. }
  23. else {
  24. testObject[itemPropertyName] = item;
  25. delete item.duplicate;
  26. }
  27. });
  28.  
  29. return duplicateFound;
  30. }
  31.  
  32.  
  33. console.log('The Array before processing: ', myArray);
  34. console.log('Duplicates found in the array: '+ checkDuplicates('name', arrayCopy));
  35. console.log('The Array after processing: ', arrayCopy );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement