![](/img/trans.png)
[英]Javascript - get the value of a property within a specific JSON array element by its key
[英]Traversing JSON with javascript to find a specific value within an array which is inside a key
所以我正在嘗試訪問外部JSON對象中的特定值,這是此特定結構:
{
resource: "playerdashboardbygeneralsplits",
parameters: {},
resultSets: [
{
name: "OverallPlayerDashboard",
headers: [],
rowSet: [
[
"Overall",
"2018-19",
60,
37,
23,
0.617,
2247.0583333333334,
651,
1483,
0.439,
294,
809,
0.363,
603,
687,
0.878,
55,
334,
389,
451,
314,
130,
45,
91,
196,
452,
2199,
119,
3553.3,
23,
6,
1,
1
]
]
},
{},
{},
{},
{},
{},
{}
]
}
我可以一直顯示rowSet
的第一個孩子,這給了我數組。 從這里,我不確定如何在此數組中返回第27(2199)個值。
我的js文件的完整內容如下:
var express = require('express') var fetch = require('isomorphic-fetch') var app = express() async function nbaFetch(){ let result = await fetch('https://stats.nba.com/stats/playerdashboardbygeneralsplits?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerID=201935&PlusMinus=N&Rank=N&Season=2018-19&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=general&VsConference=&VsDivision=', { mode: 'cors', method: "GET", // *GET, POST, PUT, DELETE, etc. headers: { "accept-encoding": "Accepflate, sdch", "accept-language": "he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4", "cache-control": "max-age=0", connection: "keep-alive", host: "stats.nba.com", referer: "http://stats.nba.com/", "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" }, }) let nbaFileStruct = await result.json() return nbaFileStruct } app.use('/', async function (req, res, next) { let result = await nbaFetch().catch(error => console.log(error)) res.send(JSON.stringify(result.resultSets[0].rowSet[0])) }) app.listen(3001, console.log("I'm a server and I am listening on port 3001"))
有沒有一種簡單的方法可以訪問嵌套數組中的值? 我搜索了網站的其余部分,但找不到此特定問題的答案。
任何幫助將不勝感激。
您可以指定數組的索引。 在這種情況下,數組中的第27位是索引26。
const result = { resource: "playerdashboardbygeneralsplits", parameters: {}, resultSets: [{ name: "OverallPlayerDashboard", headers: [], rowSet: [ [ "Overall", "2018-19", 60, 37, 23, 0.617, 2247.0583333333334, 651, 1483, 0.439, 294, 809, 0.363, 603, 687, 0.878, 55, 334, 389, 451, 314, 130, 45, 91, 196, 452, 2199, 119, 3553.3, 23, 6, 1, 1 ] ] }, {}, {}, {}, {}, {}, {} ] } console.log(result.resultSets[0].rowSet[0][26])
在Javascript中,當您使用[0]
表示法獲得數組數組的第一個元素時,它將返回一個數組,因此您只需將[]
與所需的索引一起使用。
result.resultSets[0].rowSet[0][26]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.