我对这段代码感到头疼:
{
"Carrefour": [
{ "geography": [ "Europe", "Germany" ], "productLine": "Produce", "revenue": { "2022": 130143, "2021": 172122, "2020": 103716 } },
{ "geography": [ "Europe", "France" ], "productLine": "Clothing", "revenue": { "2022": 85693, "2021": 91790, "2020": 77650 } },
],
"Tesco": [
{ "geography": [ "Europe", "United Kingdom" ], "productLine": "Electronics", "revenue": { "2022": 239537, "2021": 131959, "2020": 97047 } },
{ "geography": [ "Europe", "Ireland" ], "productLine": "Produce", "revenue": { "2022": 74963, "2021": 43406, "2020": 54623 } },
]
}
我正在尝试遍历此代码以获取所有内容。
但首先我不知道如何到达该地区(欧洲)。我试过了
function (json) {
for (var company in json) {
region = json[company].geography[0];
}
}
但它返回 undefined,因为 company 是一个数组。Infactjson.Carrefour[0].geography[0];
正确返回“欧洲”。
如何以数组形式访问公司并保留名称?我不能直接使用这个名字!
谢谢你。