How To Remove Duplicate Objects from Array in JQuery

How To Remove Duplicate Objects from Array in JQuery

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

As well as, This tutorial will also guide us on an easy and simple way to remove duplicate objects from an 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.

How To Remove Duplicate Objects from Array in JQuery

Here is an example for 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 executing the following code, the following jQuery code will remove duplicate objects from the 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.

Recommended jQuery Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *