gpt4 book ai didi

javascript - 使用未命名数组

转载 作者:行者123 更新时间:2023-11-29 19:26:04 25 4
gpt4 key购买 nike

我正在调用 API 并成功取回这样的数组:

[ {
"absmag" : "4.85",
"speed" : "0",
"colorb_v" : "0.65",
"label" : "Sun",
"appmag" : "-26.72",
"distance_light_years" : "0",
"id" : "53794",
"hipnum" : "0",
"vy" : "0",
"updated_at" : "49:09.3",
"vx" : "0",
"vz" : "0",
"texnum" : "1",
"plxerr" : "0",
"created_at" : "49:09.3",
"plx" : "0",
"dcalc" : "0",
"z" : "0",
"luminosity" : "0.8913",
"y" : "0",
"x" : "0"
}
]

如何引用这些行中的每一行?我想做类似的事情:

var database = xml.responseText;
console.log(database.label);

最佳答案

xml.responseText 是一个数组,在显示label 之前需要访问好索引:

var database = xml.responseText;
console.log(database[0].label); // Add [0] because your example is an array of one element if you have more index refer to the edit

如果响应是一个字符串,你需要在使用之前解析响应:

var database = JSON.parse(xml.responseText);
console.log(database[0].label);

编辑:

如果你的数组有多个索引,你可以使用 foreach循环:

database.forEach(function(el) {
console.log(el.label);
})

关于javascript - 使用未命名数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30737534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com