我如何从给定的对象访问'line‘数据?这是到api -https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json的链接。
{
"events": [{
"id": 153502679,
"name": "Green Bay Packers @ Chicago Bears",
"homeTeamName": "CHI Bears",
"awayTeamName": "GB Packers",
"startDate": "2019-09-06T00:20:00.0000000Z",
"offers": [{
"id": "1-2160521227",
"label": "Point Spread",
"outcomes": [{
"id": "1-2568051855",
"label": "GB Packers",
"line": "+3.5000",
"oddsAmerican": "-110",
"oddsDecimal": 1.9100,
"oddsFractional": "10/11",
"participant": "GB Packers"
},
我得到了
<td>{{data.name }}</td>
<td>{{data.homeTeamName }}</td>
<td>{{data.awayTeamName}}</td>
<td>{{data.startDate }}</td>
<!--<td>{{data.offers[1].label }}</td>-->
<td>{{data.offers.outcomes[2].line }}</td>
我尝试了上面的代码通过它访问结果对象中的行,给我下面的错误
ERROR TypeError: Cannot read property '2' of undefined
发布于 2019-06-13 03:23:16
看起来您没有访问优惠数组中的数据。
请尝试以下代码来访问事件,然后是优惠,然后是结果:
data.events[0].offers[0].outcomes[0].line
希望它能起作用!
发布于 2019-06-13 03:24:27
提供的Api响应包含3个嵌套数组。因此,在访问它们的值时,您必须考虑它们。根据您在下面数组中的项目数量,解决方案会有所不同。在您的示例中,您访问的是数组“Events”、“offers”、“outcomes”的第一项的值。
在此特定情况下,您可以尝试
<td>{{data.events[0].offers[0].outcomes[0].line}} </td>
发布于 2019-06-13 03:21:24
考虑到在每个嵌套的aray中只有一项,您可以像这样访问line
let data = {
"events": [{
"id": 153502679,
"name": "Green Bay Packers @ Chicago Bears",
"homeTeamName": "CHI Bears",
"awayTeamName": "GB Packers",
"startDate": "2019-09-06T00:20:00.0000000Z",
"offers": [{
"id": "1-2160521227",
"label": "Point Spread",
"outcomes": [{
"id": "1-2568051855",
"label": "GB Packers",
"line": "+3.5000",
"oddsAmerican": "-110",
"oddsDecimal": 1.9100,
"oddsFractional": "10/11",
"participant": "GB Packers"
}]
}]
}]
}
console.log(data.events[0].offers[0].outcomes[0].line);
如果每个嵌套数组中有n个对象,您可以这样做
let data = {
"events": [{
"id": 153502679,
"name": "Green Bay Packers @ Chicago Bears",
"homeTeamName": "CHI Bears",
"awayTeamName": "GB Packers",
"startDate": "2019-09-06T00:20:00.0000000Z",
"offers": [{
"id": "1-2160521227",
"label": "Point Spread",
"outcomes": [{
"id": "1-2568051855",
"label": "GB Packers",
"line": "+3.5000",
"oddsAmerican": "-110",
"oddsDecimal": 1.9100,
"oddsFractional": "10/11",
"participant": "GB Packers"
}]
}]
}]
}
data['events'].forEach(event => {
event.offers.forEach(offer => {
offer.outcomes.forEach(outcome => {
console.log(outcome.line);
});
});
});
https://stackoverflow.com/questions/56568796
复制相似问题