在JavaScript中以数组形式获取JSON值



我有JSON内容如下

jsonList = {
"0": {
"Name": "Virat Kohli",
"Runs": "6283",
"Matches": "207",
"Average": "37.39",
"Strike Rate": "129.94",
"Half Centuries": "42",
"Centuries": "5",
"Team": "Royal Challengers Bangalore"
},
"1": {
"Name": "Shikhar Dhawan",
"Runs": "5784",
"Matches": "192",
"Average": "34.84",
"Strike Rate": "126.64",
"Half Centuries": "44",
"Centuries": "2",
"Team": "Delhi Capitals"
},
"2": {
"Name": "Rohit Sharma",
"Runs": "5611",
"Matches": "213",
"Average": "31.17",
"Strike Rate": "130.39",
"Half Centuries": "40",
"Centuries": "1",
"Team": "Mumbai Indians"
},
"3": {
"Name": "Suresh Raina",
"Runs": "5528",
"Matches": "205",
"Average": "32.51",
"Strike Rate": "136.76",
"Half Centuries": "39",
"Centuries": "1",
"Team": "Chennai Super Kings"
}
}

我得到了"Virat Kohli">

name = jsonList[0].Name

我正在努力得到所有的名字在一个数组和运行在一个数组,如

names = ['Virat Kohli','Shikhar Dhawan','Rohit Sharma','Suresh Raina']
runs = [6283,5784,5611,5528]

我知道我可以使用for循环来做到这一点。但是还有比for循环更有效的方法吗?

首先,将Obj转换为数组:

const arr = Object.values(jsonList)

然后,使用。map函数获取所需的信息:

const names = arr.map(el=> el.Name)
const runs = arr.map(el=> el.Runs)

相关内容

  • 没有找到相关文章

最新更新