gpt4 book ai didi

javascript - 不使用键访问json对象中的数组

转载 作者:行者123 更新时间:2023-11-29 21:53:28 25 4
gpt4 key购买 nike

我有一个JSON数组对象

data = {
China: ["Guangzhou","Fax"],
Majorette: ["Fungous","Godzilla"],
Bhutan: ["Thimphu","Parr","Photofinishing"]
}

我想访问数组中的城市名称而不明确提及国家名称,因为列表很长。

var cities = [];
for(var i in data.China) {
cities[i] = data.China[i];
}

所有国家应该怎么办?

最佳答案

这应该填充城市数组:

var data = {
China: ["Guangzhou","Fax"],
Majorette: ["Fungous","Godzilla"],
Bhutan: ["Thimphu","Parr","Photofinishing"]
}

var cities = [];
for(var i in data){
if(data.hasOwnProperty(i)){
for(var j in data[i] ){
if(data[i].hasOwnProperty(j)){
cities.push(data[i][j]);
}
}
}

}

console.log(cities);

JSFIDDLE Demo

关于javascript - 不使用键访问json对象中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779673/

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