Remove duplicate objects from array in jQuery; Through this tutorial, i am going to show you how to remove duplicate objects from array in jQuery.
jQuery Remove Duplicate Objects from Array
You can use the following methods to remove duplicate objects from array:
- Method 1 – How To Remove Duplicate Objects from Array in jQuery
Method 1 – How To Remove Duplicate Objects from Array in jQuery
See the following example for how to remove duplicate objects from array in jQuery:
<!DOCTYPE html> <html> <head> <title>Remove Duplicate Objects from Array Jquery Example - Laratutorials.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>
The following script 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>
Recommended jQuery Tutorials
Recommended:-jQuery Filter Selection of HTML Elements
Recommended:-jQuery Find Sibling with id, Class
Recommended:-jQuery scrollTop Method
Recommended:-jQuery Load External Html Content from Another Page
Recommended:-jQuery Ajax Post Example
Recommended:-jQuery Get/Find Closets Element
Recommended:-jQuery Check Uncheck Checkbox
Recommended:-jQuery Previous Next Sibling Methods
Recommended:-jQuery setInterval and setTimeout
Recommended:-jQuery Select Multiple Classes
Recommended:-jQuery Registration Form validation using Validator
Recommended:-jQuery Form Validation Custom Error Message
Recommended:-jQuery Get Set and Delete Div Background Image