How to Remove Duplicate Object from Array in JQuery?
Today, our topic is how to remove duplicates items from multidimensional array in jquery. i will help to remove duplicates from an array of objects in javascript. it's very simple to delete duplicate object from json array in jquery.
After long time i am writing small jquery post for jquery array. i always prefer to write every topic that might be help to other developer. same point of view i am going to write this small post for how to remove duplicates object or array from array.
In this example we will create one helper function called "removeDumplicateValue()". in this function we use each loop remove duplicate items from array and create new array. you can see very small and basic example for this.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Remove Duplicate Objects from Array Jquery Example - ItSolutionstuff.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" : "Hardik", "lastName" : "Savani" },
{ "id" : "2", "firstName" : "Vimal", "lastName" : "Kashiyani" },
{ "id" : "3", "firstName" : "Harshad", "lastName" : "Pathak" },
{ "id" : "1", "firstName" : "Hardik", "lastName" : "Savani" },
{ "id" : "5", "firstName" : "Harsukh", "lastName" : "Makawana" }
];
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>
Output:
Array(4)
0: {id: "1", firstName: "Hardik", lastName: "Savani"}
1: {id: "2", firstName: "Vimal", lastName: "Kashiyani"}
2: {id: "3", firstName: "Harshad", lastName: "Pathak"}
3: {id: "5", firstName: "Harsukh", lastName: "Makawana"}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- JQuery Add Remove Input Fields Dynamically Example
- JQuery Select Box with Search using Select2 JS Example
- JQuery Timepicker with AM PM Format Example
- JQuery Bootbox Modal Dialog Prompt Example
- JQuery Notification Popup Box using Toastr JS Example
- How to Get IP Address in JQuery?
- JQuery Highlight Text by Search and Match Example
- Jquery Fadein Fadeout Event Example
- How to Check Image Loaded or Not in JQuery?
- Jquery Ajax Request Example Tutorial
- How to Remove undefined Value from Array in JQuery?
- Automatically Scroll to Bottom of Div JQuery Example
- How to Allow Only One Checkbox Checked at a Time in JQuery?
- How to Check Undefined, Empty and Null in JQuery?
- How to Check Object is Empty or Not in JQuery?