如何使用JavaScript / jquery在不知道键的情况下从多维json数组检索值?

时间:2019-07-12 02:10:43

标签: javascript jquery arrays json

我有一个包含多维数组的json文件。第一层是城市,第二层是温度数据。我无法动态地从第二层提取值。

我尝试了以下操作:

console.log(Object.values(json[0])[3][1]) but it gives me errors.

这可以正常工作:

console.log(Object.values(json[0])[3])

以下我的JSON代码示例:

[
  {
    "id": 1,
    "city": "Amsterdam",
    "country": "Netherlands",
    "monthlyAvg": [
      {
        "high": 7,
        "low": 3,
        "dryDays": 19,
        "snowDays": 4,
        "rainfall": 68
      },
      {
        "high": 6,
        "low": 3,
        "dryDays": 13,
        "snowDays": 2,
        "rainfall": 47
      },
      {
        "high": 10,
        "low": 6,
        "dryDays": 16,
        "snowDays": 1,
        "rainfall": 65
      },
      {
        "high": 11,
        "low": 7,
        "dryDays": 12,
        "snowDays": 0,
        "rainfall": 52
      },
      {
        "high": 16,
        "low": 11,
        "dryDays": 15,
        "snowDays": 0,
        "rainfall": 59
      },
      {
        "high": 17,
        "low": 11,
        "dryDays": 14,
        "snowDays": 0,
        "rainfall": 70
      },
      {
        "high": 20,
        "low": 12,
        "dryDays": 14,
        "snowDays": 0,
        "rainfall": 74
      },
      {
        "high": 20,
        "low": 12,
        "dryDays": 15,
        "snowDays": 0,
        "rainfall": 69
      },
      {
        "high": 17,
        "low": 10,
        "dryDays": 14,
        "snowDays": 0,
        "rainfall": 64
      },
      {
        "high": 14,
        "low": 9,
        "dryDays": 16,
        "snowDays": 0,
        "rainfall": 70
      },
      {
        "high": 9,
        "low": 6,
        "dryDays": 20,
        "snowDays": 1,
        "rainfall": 82
      },
      {
        "high": 7,
        "low": 1,
        "dryDays": 19,
        "snowDays": 1,
        "rainfall": 85
      }
    ]
  },
  {
    "id": 2,
    "city": "Athens",
    "country": "Greece",
    "monthlyAvg": [

我希望能够检索到对应于高的值7。 我目前可以这样说:

json[0].monthlyAvg[0].high

如何在不指定“ .high”的情况下获得结果

例如,这就是我想像的代码: 像这样:

“高”:7->我想输入json [0] .monthlyAvg [0] [0]

“低”:3->我想通过输入json [0] .monthlyAvg [0] [1]

来获得7

“ dryDays”:19->我想通过输入类似json [0] .monthlyAvg [0] [2]

来获得7

“ snowDays”:4->我想通过输入json [0] .monthlyAvg [3] [0]

来获得7

“降雨”:68->我想输入json [0] .monthlyAvg [0] [4]

2 个答案:

答案 0 :(得分:0)

我认为您正在以错误的方式解决该问题,因为有更多方便的方法可以遍历数组。 看一下这段代码,您可以在这里找到一个有效的示例: https://codesandbox.io/s/wonderful-grothendieck-2rjze

jsonData.forEach(cityObject => {
  console.log(
    `*** Printing data for ${cityObject.city}, ${cityObject.country} ***`
  );

  cityObject.monthlyAvg.forEach(avg => {
    let stringStat = "";

    for (const key in avg) {
      let value = avg[key];

      if (stringStat.length > 0) stringStat += ", ";

      stringStat += `${key}: ${value}`;
    }

    console.log(stringStat);
  });
});

答案 1 :(得分:-1)

Object.values(Object.values(json[0])[3][0])

现在您可以选择0、1、2、3、4的高,低,干等。

示例

Object.values(Object.values(json[0])[3][0])[0]

将返回7(从每月阿姆斯特丹的平均高点开始)

说明

let flattenedJson = Object.values(json[0]);

这将返回您所说的数组中的值。但是,thrid索引仍然是一个对象。

let monthlyAVG = flattenedJson[3][n];

其中n是要选择的数据的索引。这将返回带有高,低,dryDay等键的对象。

let flattenedData = Object.values(monthlyAVG);

现在,我们将前一个对象展平为任何数组。

let high = flattenedData[0]

这是它的工作方式。希望有道理。