Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
how to find duplicates in json array
Posted
Updated 13-Jul-16 2:25am
v2
Comments
Karthik_Mahalingam 1-Jun-16 8:03am    
what is the json format,
post sample json data
Karthik_Mahalingam 1-Jun-16 8:49am    
this doesnt look like json.
Karthik_Mahalingam 1-Jun-16 9:32am    
ok,

First of all, I don't know why copying and pasting JSON markup was such a hard task but oh well I'll do my best with what you provided. In your example code you use the variable name "i" for the first loop but then refer to it inside the loop as "i1" so that would cause problems. Also it is a good idea to use the jQuery method parseJSON for compatibility reasons. Based on what you asked and the examples you provided it seems that you want to check the optionname value to make sure it is unique. You could create an array of the option names as you iterate your loops:

JavaScript
var parsed = $.parseJSON(jsonString);
var allNames = [];
for(var i in parsed.menus)
{
   for(var j in parsed.menus[i].menuoptions)
    {
        var name = parsed.menus[i].menuoptions[j].optionname;
       if(!allNames.includes(name))
        {
            allNames.push(parsed.menus[i].menuoptions[j].optionname);
        }
        else
        {
            // duplicate found
        }
    }
}

and then handle duplicates however you were going to.
 
Share this answer
 
Can't decide if your JSON is invalid or I have no idea of JSON? Anyways, if you have valid JSON and want to find unique objects from array use the following code:

JavaScript
var optionsList = [{
                "optionName": "Cricket",
                "optionupdate": "new"

            }, {
                "optionName": "Cricket",
                "optionupdate": "new"
            }, {
                "optionName": "Football",
                "optionupdate": "new"
            }, {
                "optionName": "Basketball",
                "optionupdate": "new"
            }, {
                "optionName": "Basketball",
                "optionupdate": "new"
            }, {
                "optionName": "Hockey",
                "optionupdate": "new"
            }];
            var tempArray = [],
            uniqueValueCollection = [];

            $.each(optionsList, function (index, value) {
                if ($.inArray(value.optionName, tempArray) === -1) {//Check if the object is in tempArray or not. If it is not put in in tempArray and also into uniqueValueCollection. If it is there in the tempArray, skip to the next object.
                    tempArray.push(value.optionName);
                    uniqueValueCollection.push(value);
                }
            });
            var result = JSON.stringify(uniqueValueCollection);


Here the result variable will have unique objects(based on optionName) as a string.

Hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900