How to Remove JSON Duplicates Array in Javascript?

Nov 05, 2022 . Admin



Hello friends,

This tutorial will give you a simple example of removing duplicate JSON object array javascript. I explained simply how javascript remove duplicate JSON object array. you will learn to remove duplicate JSON objects from the array in this article.

To remove the duplicates, you can use the filter() method to include only elements whose indexes match their indexOf values. Now, let's see the article on how to remove duplicate JSON objects from array javascript. you will learn to remove duplicate JSON objects from the array.

So, let's see bellow solution:

Example
index.html
<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>How to Remove JSON Duplicates Array in Javascript?</title>
</head>
<body>
   <div id="output" class="output"></div>
   <script type="text/javascript">
        var data=[
            {
                "email": "Vasu@gmail.com",
                "message": "Hi",
                "name": "Vasudev",
                "time": 1505224869596,
                "type": "anoynomous",
                "userid": "1"
            },
            {
                "email": "raghav@gmail.com",
                "message": "Hello",
                "name": "Vasudev",
                "time": 1505223925715,
                "type": "anoynomous",
                "userid": "1"
            },
            {
                "email": "shivam@gmail.com",
                "message": "Hello",
                "name": "Shivam",
                "time": 1505223925715,
                "type": "anoynomous",
                "userid": "3"
            }
        ]
        var temp=[ ]
        data=data.filter((item)=>{
            if(!temp.includes(item.userid)){
                temp.push(item.userid)
                return true;
            }
        });
        document.getElementById('output').innerHTML=JSON.stringify(data);
   </script>
</body>
</html>
Output:
[{"email":"Vasu@gmail.com","message":"Hi","name":"Vasudev","time":1505224869596,"type":"anoynomous","userid":"1"},
{"email":"shivam@gmail.com","message":"Hello","name":"Shivam","time":1505223925715,"type":"anoynomous","userid":"3"}]

I hope it will help you...

#Javascript