Removing the duplicates in the array of the objects is always tricky and sometimes it can be more complex, when we are going with an array of array.

Recently, I came across the scenario, where the duplicate employees are pushing into the array, when some process is going on, so that I need to write a client side jQuery script to clean up the array; i.e., removing the duplicate entries.

Code
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <meta charset="utf-8" />
  6. <script src="https://code.jquery.com/jquery-3.0.0.js" ></script>
  7. </head>
  8. <body>
  9. <script type="text/javascript">
  10. var Array = [{ "Name": "Bob Ross", "EmpID": "0441" },
  11. { "Name": "Ram", "EmpID": "0442" },
  12. { "name": "Bob Ross", "EmpID": "0441" },
  13. { "name": "Pradeep", "text": "0443" },
  14. { "name": "Ram", "text": "0442" }];
  15. ArrayArray = Array.reduce(function (item, e1) {
  16. var matches = item.filter(function (e2)
  17. { return e1.EmpID == e2.EmpID});
  18. if (matches.length == 0) {
  19. item.push(e1);
  20. }
  21. return item;
  22. }, []);
  23. console.log(Array);
  24. </script>
  25. </body>
  26. </html>
Result


There are many methods available to remove the duplicate values from an array but from the scenario I came across, I found this is the most efficient and generic way to work on it.

I hope you enjoyed this blog. Your valuable feedback, questions or comments about this blog are always welcome.