Javascript 通过JSON对象循环以获取数组的内容

Javascript 通过JSON对象循环以获取数组的内容,javascript,arrays,json,Javascript,Arrays,Json,我被这段代码弄得头晕目眩: { "Carrefour": [ { "geography": [ "Europe", "Germany" ], "productLine": "Produce", "revenue": { "2022": 130143, "2021": 172122, "2020": 103716 } }, { "geography": [ "Europe", "France" ], "productLine": "Clothing", "revenue": {

我被这段代码弄得头晕目眩:

{
  "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 } },
    ]
}
我正在尝试循环使用此代码以获取所有内容

但首先,我不知道如何到达该地区(欧洲)。我试过了

但它返回未定义,因为company是一个数组。事实
json.Carrefour[0].地理[0]正确返回“欧洲”

如何作为数组访问公司并保留名称?我不能直接用这个名字


谢谢。

您的公司分配了阵列,因此您需要一个内部循环

for (var company in json) {
    for (var i = 0; i < json[company].length; i++) {
        region = json[company][i].geography[0];
    }
}
for (var company in json) {
    for (var i = 0; i < json[company].length; i++) {
        region = json[company][i].geography[0];
    }
}
Carrefour: 
    Europe
    Europe
Tesco: 
    Europe
    Europe