简体   繁体   English

如何使用2个值过滤JSON数据

[英]How to filtering JSON data with 2 values

I had a json response: 我有一个json响应:

var json = {
    "map": [{
        "title": "Red",
        "link": "red.html",
        "subPageArray": [{
            "subTitle": "SubLink1",
            "link": "test1.html"
        }, {
            "subTitle": "SubLink1",
            "link": "test9.html"
        }, {
            "subTitle": "SubLink2",
            "link": "test2.html"
        }]
    },
    {
        "title": "Blue",
        "link": "blue.html",
        "subPageArray": [{
            "subTitle": "SubLink1",
            "link": "test1.html"
        }, {
            "subTitle": "SubLink2",
            "link": "test2.html"
        }]
    }, {
        "title": "Green",
        "link": "green.html"
    }, {
        "title": "Yellow",
        "link": "yellow.html",
        "subPageArray": [{
            "subTitle": "SubLink1",
            "link": "test1.html"
        }, {
            "subTitle": "SubLink2",
            "link": "test2.html"
        }]
    }]
}

How to filter this json with 2 parameters, I want to get json data based on title: Red with subTitle: Sublink1. 如何使用2个参数过滤此json,我想根据标题:带有subTitle:Sublink1的红色获取json数据。 Currently I had only filtering one value: 目前,我只过滤一个值:

$.each(json.map, function(i, val) { 
  if (val["title"] === "Red") { 
     var data = "<li class='topLevel'><a href='/" + this.link + "'>" + this.title + "</a><ul>"; 
     if (this.subPageArray != undefined) { // to make sure subPageArray exists 
       for (i = 0; i < this.subPageArray.length; ++i) { 
         data += "<li class='subLevel'><a href='/" + this.subPageArray[i].link + "'>" + this.subPageArray[i].subTitle + "</a></li>"; 
       } 
     } 
     data += "</ul></li>"; 
     $(data).appendTo(".siteMap Content .hii"); 
   } 
});

Is it possible, I tried several way and my browser being unresponsive. 是否有可能,我尝试了几种方法,但我的浏览器没有响应。

Well, you are already looping over subPageArray , so why not check if this.subPageArray[i].subTitle is what you are looking for? 好了,您已经在遍历subPageArray ,那么为什么不检查this.subPageArray[i].subTitle是否在寻找呢?

$.each(json.map, function(i, val) { 
  if (val["title"] === "Red") { 
     var data = "<li class='topLevel'><a href='/" + this.link + "'>" + this.title + "</a><ul>"; 
     if (this.subPageArray != undefined) { // to make sure subPageArray exists 
       for (i = 0; i < this.subPageArray.length; ++i) { 
         // add here a validation that you are on the right subTitle
         data += "<li class='subLevel'><a href='/" + this.subPageArray[i].link + "'>" + this.subPageArray[i].subTitle + "</a></li>"; 
       } 
     } 
     data += "</ul></li>"; 
     $(data).appendTo(".siteMap Content .hii"); 
   } 
});

Or you can always call .map again on a new array. 或者,您随时可以在新数组上再次调用.map

First step we can filter title and get the list, Next can go for next inner condition to filter the inner title. 第一步,我们可以过滤标题并获得列表,下一步可以进入下一个内部条件以过滤内部标题。

Tags can be assigned based on the below condtions. 可以根据以下条件分配标签。

Check the below code to filter your json variable. 检查以下代码以过滤您的json变量。

json.map = json.map.filter(function(filterTitle){return filterTitle.title == 'Red'});
json.map = json.map.map(function(filterInnerTitle){
    filterInnerTitle.subPageArray = filterInnerTitle.subPageArray.filter(function(filterSubTitle){
        return filterSubTitle.subTitle === "SubLink1"; 
    });
    return filterInnerTitle;
})
console.log(json);

Hope this helps 希望这可以帮助

 $.each(json.map, function(i, val) { if (val["title"] === "Red" && val.subPageArray != undefined && val.subPageArray.__proto__.constructor.name == "Array") { newSubPageArray = val.subPageArray.filter((value, index)=>{ if(value.subTitle == "SubLink1"){ return true; } else { return false; } }); if(newSubPageArray.length > 0){ var data = "<li class='topLevel'><a href='/" + this.link + "'>" + this.title + "</a><ul>"; $.each(newSubPageArray,function(index, value){ data += "<li class='subLevel'><a href='/" + value.link + "'>" + value.subTitle + "</a></li>" }) data += "</ul></li>"; } $(data).appendTo(".siteMap Content .hii"); } }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM