How To Remove Duplicate Objects from Array JQuery?

Jquery

Nicesnippets

130

01-04-2021


How To Remove Duplicate Objects from Array JQuery?

Hello Friends,

In this blog, I will show how to remove duplicate objects from array usinig jquery. We will learn jquery remove duplicate objects from array.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.

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.

Here i will give you full example for how to remove duplicate objects from array usinig jquery So let's see the bellow example:

Example :


<!DOCTYPE html>

<html>

<head>

<title>Remove Duplicate Objects from Array Jquery Example - NiceSnippets.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" : "Rahul", "lastName" : "Shah" },

{ "id" : "2", "firstName" : "Pratik", "lastName" : "Gupta" },

{ "id" : "3", "firstName" : "Jayesh", "lastName" : "Pandya" },

{ "id" : "4", "firstName" : "Raghav", "lastName" : "Patel" },

{ "id" : "2", "firstName" : "Pratik", "lastName" : "Gupta" }

];

function remove_Dumplicate_Value(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(remove_Dumplicate_Value(myArray));

</script>

</body>

</html>

It will help you....


Recommended Posts