Search code examples
javascriptjquerylogicbusiness-logic

In a given Json, search for the particular given string based on key, return true if found, else false Using jQuery/Javascript


Given JSON String/object

{
    "selectAll": false,
    "include": {
        "country_197": {
            "id": "197",
            "data_type": "country",
            "name": "Singapore",
            "desc": "",
            "parent_key_id": "all_all",
            "status": ""
        },
        "country_100": {
            "id": "100",
            "data_type": "country",
            "name": "India",
            "desc": "",
            "parent_key_id": "all_all",
            "status": ""
        }
    },
    "exclude": {
        "state_2": {
            "id": "2",
            "data_type": "state",
            "name": "Andhra Pradesh",
            "desc": "",
            "parent_key_id": "country_100",
            "status": ""
        }
    }
}

Given search string is: country_100

Required : Have to search for country_100 in Given JSON String/object by key parent_key_id Ex: searching country_100 is found :

{
    "selectAll": false,
    "include": {
        "country_197": {
            "id": "197",
            "data_type": "country",
            "name": "Singapore",
            "desc": "",
            "parent_key_id": "all_all",
            "status": ""
        },
        "country_100": {
            "id": "100",
            "data_type": "country",
            "name": "India",
            "desc": "",
            "parent_key_id": "all_all",
            "status": ""
        }
    },
    "exclude": {
        "state_2": {
            "id": "2",
            "data_type": "state",
            "name": "Andhra Pradesh",
            "desc": "",
            "parent_key_id": "**country_100**",
            "status": ""
        }
    }
}

So return True, else return false.
This is what I have so far

var id = 'country_100', found = false;
for (var i=0; i<data.length; i++) { 
    console.log(data[i].exclude['state_2'].parent_key_id); 
    if (data[i].exclude['state_2'].parent_key_id == id) { 
        found = true; 
        break; 
    }
}

Solution

  • Here ya go! http://jsfiddle.net/8p9KF/

    <script type="text/javascript">
    onerror=function(a,b,c){alert([a,b,c])};
    </script>
    <script type="text/javascript">
    var myJSON={
      "selectAll":false,
      "include":
      {
        "country_197":{"id":"197","data_type":"country","name":"Singapore","parent_key_id":"all_all"},
        "country_100":{"id":"100","data_type":"country","name":"India","parent_key_id":"all_all"}
      },
      "exclude":
      {"state_2":
        {"id":"2","data_type":"state","name":"Andhra Pradesh","parent_key_id":"country_100"}
      }
    };
    
    //alert(myJSON.include["country_100"].data_type=="country");
    
    function countryIsIncluded(country)
    {
        var keyExists=(country in myJSON.include);
        if(!keyExists)return false;
        return (myJSON.include[country].data_type=="country");
    }
    
    function countryIsExcluded(country)
    {
        var keyExists=(country in myJSON.exclude);
        if(!keyExists)return false;
        return myJSON.exclude[country].data_type=="country";
    }
    
    function stateIsIncluded(state)
    {
        var keyExists=(state in myJSON.include);
        if(!keyExists)return false;
        return myJSON.include[state].data_type=="state";
    }
    
    function stateIsExcluded(state)
    {
        var keyExists=(state in myJSON.exclude);
        if(!keyExists)return false;
        return myJSON.exclude[state].data_type=="state";
    }
    
    countryIdMap={};
    stateIdMap={};
    for(var j in myJSON.include){
     if(!myJSON.include.hasOwnProperty(j))continue;
     if(myJSON.include[j].data_type=="country")countryIdMap[myJSON.include[j].name]=myJSON.include[j].id;
     else if(myJSON.include[j].data_type=="state")stateIdMap[myJSON.include[j].name]=myJSON.include[j].id;
    }
    for(var j in myJSON.exclude){
     if(!myJSON.exclude.hasOwnProperty(j))continue;
     if(myJSON.exclude[j].data_type=="country")countryIdMap[myJSON.exclude[j].name]=myJSON.exclude[j].id;
     else if(myJSON.exclude[j].data_type=="state")stateIdMap[myJSON.exclude[j].name]=myJSON.exclude[j].id;
    }
    
    
    function lookUpCountry(countryName)
    {
        var c=(countryName in countryIdMap)?"country_"+countryIdMap[countryName]:countryName;
        return {included:countryIsIncluded(c),excluded:countryIsExcluded(c)};
    }
    
    function lookUpState(stateName)
    {
        var s=(stateName in stateIdMap)?"state_"+stateIdMap[stateName]:stateName;
        return {included:stateIsIncluded(s),excluded:stateIsExcluded(s)};
    }
    
    
    var india=lookUpCountry("India");
    alert("India is:\nincluded: "+(india.included?"Y":"N")+"\nexcluded: "+(india.excluded?"Y":"N"));
    
    var country_100=lookUpCountry("country_100");
    alert("country_100 is:\nincluded: "+(country_100.included?"Y":"N")+"\nexcluded: "+(country_100.excluded?"Y":"N"));
    
    var country_197=lookUpCountry("country_197");
    alert("country_197 is:\nincluded: "+(country_197.included?"Y":"N")+"\nexcluded: "+(country_197.excluded?"Y":"N"));
    
    var country_349999=lookUpCountry("country_349999");
    alert("country_349999 is:\nincluded: "+(country_349999.included?"Y":"N")+"\nexcluded: "+(country_349999.excluded?"Y":"N"));
    
    var state_2=lookUpState("state_2");
    alert("state_2 is:\nincluded: "+(state_2.included?"Y":"N")+"\nexcluded: "+(state_2.excluded?"Y":"N"));
    
    var andhraPradesh=lookUpState("Andhra Pradesh");
    alert("Andhra Pradesh is:\nincluded: "+(andhraPradesh.included?"Y":"N")+"\nexcluded: "+(andhraPradesh.excluded?"Y":"N"));
    
    var state_2999=lookUpState("state_2999");
    alert("state_2999 is:\nincluded: "+(state_2999.included?"Y":"N")+"\nexcluded: "+(state_2999.excluded?"Y":"N"));
    </script>