简体   繁体   English

如何将特定数据从JSON文件解析到html下拉菜单

[英]How to parse specific data from JSON file to a html dropdown menu

JSON FILE JSON文件

Hi, in the json file i have shown above there is over 30 matchdays and i was wondering how would i be able to parse the data from the JSON file to a html file and have the options in a dropdown menu to be able to filter by matchdays or filter by teams, is it possible to do this or am I overthinking it, i have the file parsing the data and showing all results from the json file into a table as shown below html file showing all matchdays 嗨,我在上面显示的json文件中有30多个比赛日,我想知道如何将JSON文件中的数据解析为html文件,并在下拉菜单中选择要过滤的内容比赛日或按团队进行过滤,是否有可能做到这一点,或者我是否考虑得过长,我有文件解析数据并将json文件的所有结果显示到表格中,如下所示, html文件显示了所有比赛日

i was wondering is there a way i can filter these results by specific team or matchdays ? 我想知道是否有办法按特定球队或比赛日筛选这些结果? any help appreciated thanks 任何帮助表示感谢

html file taking in the json data 接收json数据的html文件

Below code snippet would help you to finish your work. 下面的代码段将帮助您完成工作。

var json = '{"name":"EnglishPremierLeague2015","rounds":[{"name":"Matchday1:","matches":[{"date":"2015-08-08","team1":{"key":"manutd","name":"ManchesterUnite","code":"MUN"},"team2":{"key":"tottenham","name":"TottenhamHotspu","code":"TOT"},"scorer":1,"score2":0}]},{"name":"Matchday2:","matches":[{"date":"2015-08-09","team1":{"key":"India","name":"IndianCricketTeam","code":"IND"},"team2":{"key":"England","name":"EnglandCricketTeam","code":"ENG"},"scorer":1,"score2":0}]}]}';

var data = JSON.parse(json);

// Filter by Match Day
var filter = $(data.rounds).filter(function () { 
    return this.name == "Matchday1:";
})

// Test Case :)
if (filter.length == 1) {
    console.log("Filter working")
}

var filterbyTeam = $(data.rounds).filter(function () {
    return this.matches.filter(function (e) {
        return e.team1.key == "India" || e.team2.key == "India";
    }).length >= 1;
})

// Test Case :)
if (filterbyTeam.length == 1) {
    console.log("Filter working")
}

This code will get the JSON array with the filter that you can use further to show your HTML. 此代码将获得带有过滤器的JSON数组,您可以将其进一步用于显示HTML。

Note: You need to pass filter text according to your requirement. 注意:您需要根据需要传递过滤器文本。 You can change the attribute as well to filter specific data. 您也可以更改属性以过滤特定数据。

Hope this will help :) 希望这会有所帮助:)

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

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