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

时间:2017-12-15 12:23:00

标签: javascript json

我有一个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"
        }]
    }]
}

如何使用2个参数过滤此json,我想根据标题获取json数据:带子标题的红色:Sublink1。目前我只过滤了一个值:

$.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"); 
   } 
});

是否有可能,我尝试了几种方式,我的浏览器没有响应。

3 个答案:

答案 0 :(得分:0)

嗯,您已经在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"); 
   } 
});

或者您可以随时在新阵列上再次呼叫.map

答案 1 :(得分:0)

第一步我们可以过滤标题并获取列表,接下来可以进入下一个内部条件来过滤内部标题。

可以根据以下条件分配标签。

检查以下代码以过滤您的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);

答案 2 :(得分:0)

希望这有帮助

$.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"); 
		}
};