2 回答
TA贡献1812条经验 获得超5个赞
你可以做
let countryData = {
"France": ["Nouvelle-Aquitaine",
"Occitanie",
"Pays de la Loire",
"Provence-Alpes-Côte d'Azur"
],
"India": ["West Bengal",
"Andhrapradesh",
"Hariyana"
],
"Bangladesh": ["Dhaka",
"Khulna",
"Barishal"
]
};
var country = countryData['France'];
for (idx in country) {
var letter = country[idx][0];
console.log(letter);
}
然后遍历country变量以获得数组中每个元素的第一个字母
TA贡献1826条经验 获得超6个赞
这是无效的,if(countryData[info] == stateName){因为info已经具有属性值。怎么查的?在工作。
你应该有 :
function findCountryFunction(stateName) {
let result = [];
for (let info in countryData[stateName]) {
result.push(countryData[stateName][info].charAt(0))
}
console.log(result.join());
}
let countryData = {
"France": ["Nouvelle-Aquitaine",
"Occitanie",
"Pays de la Loire",
"Provence-Alpes-Côte d'Azur"
],
"India": ["West Bengal",
"Andhrapradesh",
"Hariyana"
],
"Bangladesh": ["Dhaka",
"Khulna",
"Barishal"
]
};
findCountryFunction('France');
添加回答
举报