我的“传播”内部有动态数组编号(每个事件有不同的数字)。每个数组都有这个
alt_line_id: null
。我想获取哪个数组对象确实具有该值 null 值,然后将“home &away”值显示到我的 html 中。
我的示例数组:
"events": [
"periods": {
"num_0": {
"line_id": 2250607026,
"number": 0,
"cutoff": "2023-09-18T23:15:00Z",
"period_status": 1,
"money_line": {
"home": 2.5,
"draw": null,
"away": 1.598
},
"spreads": {
"3.0": {
"hdp": 3.0,
"alt_line_id": null,
"home": 2.0,
"away": 1.909,
"max": 30000.0
},
"5.5": {
"hdp": 5.5,
"alt_line_id": 36582376056,
"home": 1.625,
"away": 2.39,
"max": 30000.0
},
"5.0": {
"hdp": 5.0,
"alt_line_id": 36582376059,
"home": 1.662,
"away": 2.32,
"max": 30000.0
},
"4.5": {
"hdp": 4.5,
"alt_line_id": 36582376062,
"home": 1.694,
"away": 2.26,
"max": 30000.0
},
"4.0": {
"hdp": 4.0,
"alt_line_id": 36582376064,
"home": 1.746,
"away": 2.19,
"max": 30000.0
},
},
},
},
]
在上面的数组中,“spreads”对象具有动态数组列表,例如“3.0”就是具有
"alt_line_id": null
的对象。一旦找到,我想获取该对象的名称,以便我可以通过像这样调用它来获取“家”和“离开”:obj.events[i].periods.num_0.spreads['**my dynamic array here**'].home
这是我的代码
var xhr = new XMLHttpRequest();
xhr.open("GET", "odds.json", true);
xhr.getResponseHeader("Content-type", "application/json");
xhr.withCredentials = true;
xhr.onload = function() {
const obj = JSON.parse(this.responseText);
var out = "";
for (let i = 0; i < 1; i++) {
out += '<tr>'
+ '<td class="home_team">'+ obj.events[i].home +'</td>'
+ '<td class="home_moneyline">'+ obj.events[i].periods.num_0.money_line.home +'</td>'
+ '<td class="home_spreads">'+ obj.events[i].periods.num_0.spreads['my dynamic array here'].home +'</td>'
+ '<td class="home_totals">'+ +'</td>'
+ '<td class="home_totalgame">'
+ '<div>Over <span class="htg_over">'+ obj.events[i].periods.num_0.team_total.home.over +'</span></div>'
+ '<div>Under <span class="htg_under">'+ obj.events[i].periods.num_0.team_total.home.under +'</span></div>'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td class="away_team">'+ obj.events[i].away +'</td>'
+ '<td class="away_moneyline">'+ obj.events[i].periods.num_0.money_line.away +'</td>'
+ '<td class="away_spreads">'+ obj.events[i].periods.num_0.spreads['my dynamic array here'].away +'</td>'
+ '<td class="away_totals">'+ +'</td>'
+ '<td class="away_totalgame">'
+ '<div>Over <span class="atg_over">'+ obj.events[i].periods.num_0.team_total.away.over +'</span></div>'
+ '<div>Under <span class="atg_under">'+ obj.events[i].periods.num_0.team_total.away.under +'</span></div>'
+ '</td>'
+ '</tr>';
}
document.getElementById("odds-list").innerHTML = out;
}
for (let i = 0; i < obj.events.length; i++) {
const spreads = obj.events[i].periods.num_0.spreads;
const spreadKeys = Object.keys(spreads);
const nullSpreadKey = spreadKeys.find(key => spreads[key].alt_line_id === null);
...
}