-1

给定 JSON 字符串/对象

{
    "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": ""
        }
    }
}

给定的搜索字符串是:country_100

必需:必须country_100在给定的 JSON 字符串/对象中按键parent_key_id 搜索 例如:country_100找到搜索:

{
    "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": ""
        }
    }
}

所以返回真,否则返回假。
这是我到目前为止所拥有的

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; 
    }
}
4

3 回答 3

0

很高兴看到你格式化了对象:)

var id = 'country_100', found = false; 
if(data['exclude']['state_2']['parent_key_id'] === id) {
    found = true;
}

你为什么使用循环?除非你的对象像

data = {
    1: {"selectAll":false, "include":..., "exclude": ...},
    2: {"selectAll":false, "include":..., "exclude": ...},
    3: {"selectAll":false, "include":..., "exclude": ...}
};
于 2012-10-10T04:15:04.360 回答
0

给你! 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>
于 2012-10-10T05:21:30.777 回答
0

我想知道什么是动态的?state_2?你的意思是你不知道它是 state_2 还是 state_3,或者其他任何东西。那么你可以这样做:

for(var p in data[i].exclude){
    data[i].exclude[p];//here p is what you want
}
于 2012-10-10T05:51:40.737 回答