Remove Duplicate Objects from Array in jQuery
Jquery 12-Oct-2021

Remove Duplicate Objects from Array in jQuery

Remove duplicate objects from array in jQuery. In this tutorial, you will learn how to remove duplicate objects from array in jQuery.

As well as, This tutorial will guide you an easy and simple way to remove duplicate objects from array in jquery.

Sometimes, you may need to delete all duplicate items from an array using jquery javascript. So, this example tutorial will show you an easy way to remove duplicate objects from array in jquery.

Method 1 – How To Remove Duplicate Objects from Array in jQuery?

In this example, You will learn how to remove duplicate objects from array in jQuery:

<!DOCTYPE html>
<html>
<head>
    <title>Remove Duplicate Objects from Array Jquery Example - Tutsmake.com</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    
<script type="text/javascript">
     
  var myArray = [
        { "id" : "1", "firstName" : "Michael", "lastName" : "stoddardd" }, 
        { "id" : "2", "firstName" : "Kate", "lastName" : "smith" }, 
        { "id" : "3", "firstName" : "john", "lastName" : "sam" }, 
        { "id" : "1", "firstName" : "Kate", "lastName" : "smith" }, 
        { "id" : "5", "firstName" : "Michael", "lastName" : "stoddardd" }
      ];
     
  function removeDumplicateValue(myArray){ 
      var newArray = [];
     
      $.each(myArray, function(key, value) {
        var exists = false;
        $.each(newArray, function(k, val2) {
          if(value.id == val2.id){ exists = true }; 
        });
        if(exists == false && value.id != "") { newArray.push(value); }
      });
    
      return newArray;
    }
     
    console.log(removeDumplicateValue(myArray));
</script>
     
</body>
</html>

Note that, when execute the following code, the following jquery code will remove duplicate objects from array in jquery:

<script type="text/javascript">
     
  var myArray = [
        { "id" : "1", "firstName" : "Michael", "lastName" : "stoddardd" }, 
        { "id" : "2", "firstName" : "Kate", "lastName" : "smith" }, 
        { "id" : "3", "firstName" : "john", "lastName" : "sam" }, 
        { "id" : "1", "firstName" : "Kate", "lastName" : "smith" }, 
        { "id" : "5", "firstName" : "Michael", "lastName" : "stoddardd" }
      ];
     
  function removeDumplicateValue(myArray){ 
      var newArray = [];
     
      $.each(myArray, function(key, value) {
        var exists = false;
        $.each(newArray, function(k, val2) {
          if(value.id == val2.id){ exists = true }; 
        });
        if(exists == false && value.id != "") { newArray.push(value); }
      });
    
      return newArray;
    }
     
    console.log(removeDumplicateValue(myArray));
</script>

In the above example, use jquery each() and push function to remove duplicate objects from array in jQuery.