javascript for循环嵌套的json值

时间:2018-01-23 21:59:38

标签: javascript json for-loop highcharts

我正在尝试遍历所有totalfirst_record值的json文件。关键是

json.STATION[0].OBSERVATIONS.precipitation[0].total

我正在尝试返回结果:

[20180116, 0.8], [20180117, 0.0] . . .

我尝试了各种方法。我最好的结果是经典undefined 下面是我一直在研究的jsfiddle的片段。 json由mesowest.net api生成。

一旦我解决了这个问题,我希望能够在highcharts中绘制值。谢谢。

const data = {
  "UNITS": {
    "precipitation": "Inches"
  },
  "STATION": [{
    "STATUS": "ACTIVE",
    "MNET_ID": "25",
    "PERIOD_OF_RECORD": {
      "start": "20000120",
      "end": "20180121"
    },
    "ELEVATION": "6340",
    "NAME": "BOGUS BASIN",
    "RESTRICTED": false,
    "STID": "BOGI1",
    "ELEV_DEM": "6362",
    "LONGITUDE": "-116.09685",
    "STATE": "ID",
    "OBSERVATIONS": {
      "precipitation": [{
        "count": 23,
        "first_report": "20180115",
        "interval": 1,
        "report_type": "precip_accum",
        "last_report": "20180115",
        "total": 0.0
      }, {
        "count": 24,
        "first_report": "20180116",
        "interval": 2,
        "report_type": "precip_accum",
        "last_report": "20180116",
        "total": 0.2
      }, {
        "count": 24,
        "first_report": "20180117",
        "interval": 3,
        "report_type": "precip_accum",
        "last_report": "20180117",
        "total": 0.0
      }, {
        "count": 24,
        "first_report": "20180118",
        "interval": 4,
        "report_type": "precip_accum",
        "last_report": "20180118",
        "total": 0.0
      }, {
        "count": 24,
        "first_report": "20180119",
        "interval": 5,
        "report_type": "precip_accum",
        "last_report": "20180119",
        "total": 0.8
      }, {
        "count": 24,
        "first_report": "20180120",
        "interval": 6,
        "report_type": "precip_accum",
        "last_report": "20180120",
        "total": 0.0
      }, {
        "count": 13,
        "first_report": "20180121",
        "interval": 7,
        "report_type": "precip_accum",
        "last_report": "20180121",
        "total": 0.0
      }]
    },
    "LATITUDE": "43.76377",
    "TIMEZONE": "America\/Boise",
    "ID": "1160"
  }],
  "SUMMARY": {
    "DATA_QUERY_TIME": 1.6429424286,
    "RESPONSE_CODE": 1,
    "RESPONSE_MESSAGE": "OK",
    "METADATA_RESPONSE_TIME": "0.0920295715332 ms",
    "NUMBER_OF_OBJECTS": 1,
    "PRECIP_DATA_TIME": 2.4950504303,
    "DATA_PARSE_TIME": 0.8418560028
  }
};

   
console.log(data.STATION[0].OBSERVATIONS.precipitation[4].total);
document.getElementById("demo").innerHTML = data.STATION[0].OBSERVATIONS.precipitation[4].total;

const totl = data.STATION[0].OBSERVATIONS.precipitation[0].total;
console.log(totl);
// 

var i, precipitation;
for (i = 0; i < data.STATION[0].OBSERVATIONS.precipitation.total.length; i++) {
  precip = data.STATION[0].OBSERVATIONS.precipitation[i].total;
}
console.log(precip);
//console.log(obj);
<p id="demo"></p>

2 个答案:

答案 0 :(得分:0)

FWIW,你的密钥不是precipitation[0],但你想迭代所有降水元素:

// precipitation is an array, so it supports Array.prototype.map()

var output = data.STATION[0].OBSERVATIONS.precipitation.map(function(p) {
    return [p.first_report, p.total] });

另见:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

答案 1 :(得分:0)

首先,在for循环中,您指的是data.STATION[0].OBSERVATIONS.precipitation.total.length,但total部分甚至没有在那里定义。它必须是precipitation[0].total才能成为任何东西,即便如此,它也不是一个数组。你只想要:

data.STATION[0].OBSERVATIONS.precipitation.length

另外,只是代码风格明智,将其粘贴到变量中,这样你就不必一遍又一遍地输入那种疯狂。它还有助于避免拼写错误:

const records = data.STATION[0].OBSERVATIONS.precipitation;

您也只是将相同的值反复设置回变量,因此最后,您只能拥有最后一个值。你想把它们粘在一个数组中。

const totals = [];
for (let i = 0; i < records.length; i++) {
  totals.push(records[i].total);
}

如果您同时需要totalfirst_report,则可以执行并行数组:

const totals = [];
const firsts = [];
for (let i = 0; i < records.length; i++) {
  totals.push(records[i].total);
  firsts.push(records[i].first_record);
}

或者有一个数组数组:

const results = [];
for (let i = 0; i < records.length; i++) {
  results.push([records[i].total, records[i].first_record]);
}

这是一个有效的例子:

&#13;
&#13;
const data = {
  "UNITS": {
    "precipitation": "Inches"
  },
  "STATION": [{
    "STATUS": "ACTIVE",
    "MNET_ID": "25",
    "PERIOD_OF_RECORD": {
      "start": "20000120",
      "end": "20180121"
    },
    "ELEVATION": "6340",
    "NAME": "BOGUS BASIN",
    "RESTRICTED": false,
    "STID": "BOGI1",
    "ELEV_DEM": "6362",
    "LONGITUDE": "-116.09685",
    "STATE": "ID",
    "OBSERVATIONS": {
      "precipitation": [{
        "count": 23,
        "first_report": "20180115",
        "interval": 1,
        "report_type": "precip_accum",
        "last_report": "20180115",
        "total": 0.0
      }, {
        "count": 24,
        "first_report": "20180116",
        "interval": 2,
        "report_type": "precip_accum",
        "last_report": "20180116",
        "total": 0.2
      }, {
        "count": 24,
        "first_report": "20180117",
        "interval": 3,
        "report_type": "precip_accum",
        "last_report": "20180117",
        "total": 0.0
      }, {
        "count": 24,
        "first_report": "20180118",
        "interval": 4,
        "report_type": "precip_accum",
        "last_report": "20180118",
        "total": 0.0
      }, {
        "count": 24,
        "first_report": "20180119",
        "interval": 5,
        "report_type": "precip_accum",
        "last_report": "20180119",
        "total": 0.8
      }, {
        "count": 24,
        "first_report": "20180120",
        "interval": 6,
        "report_type": "precip_accum",
        "last_report": "20180120",
        "total": 0.0
      }, {
        "count": 13,
        "first_report": "20180121",
        "interval": 7,
        "report_type": "precip_accum",
        "last_report": "20180121",
        "total": 0.0
      }]
    },
    "LATITUDE": "43.76377",
    "TIMEZONE": "America\/Boise",
    "ID": "1160"
  }],
  "SUMMARY": {
    "DATA_QUERY_TIME": 1.6429424286,
    "RESPONSE_CODE": 1,
    "RESPONSE_MESSAGE": "OK",
    "METADATA_RESPONSE_TIME": "0.0920295715332 ms",
    "NUMBER_OF_OBJECTS": 1,
    "PRECIP_DATA_TIME": 2.4950504303,
    "DATA_PARSE_TIME": 0.8418560028
  }
};

const records = data.STATION[0].OBSERVATIONS.precipitation;

const results = [];

for (let i = 0; i < records.length; i++) {
  results.push([records[i].total, records[i].first_report]);
}

console.log(results);
&#13;
&#13;
&#13;