如何访问嵌套的json数组以获取每个对象中所有参数的所有值的列表

时间:2019-04-13 05:13:31

标签: javascript arrays json angular typescript

我有一个json类型

{
  "success": true,
  "dataPoints": [{
    "count_id": 4,
    "avg_temperature": 2817,
    "startTime": "00:00:00",
    "endTime": "00:19:59.999"
  }, {
    "count_id": 4,
    "avg_temperature": 2814,
    "startTime": "00:59:59.997",
    "endTime": "01:19:59.996"
  }, {
    "count_id": 4,
    "avg_temperature": 2816,
    "startTime": "00:39:59.998",
    "endTime": "00:59:59.997"
  }, {
    "count_id": 4,
    "avg_temperature": 2825,
    "startTime": "02:19:59.993",
    "endTime": "02:39:59.992"
  }, {
    "count_id": 4,
    "avg_temperature": 2828,
    "startTime": "02:39:59.992",
    "endTime": "02:59:59.991"
  }, {
    "count_id": 4,
    "avg_temperature": 2832,
    "startTime": "02:59:59.991",
    "endTime": "03:19:59.99"
  }, {
    "count_id": 4,
    "avg_temperature": 2841,
    "startTime": "03:39:59.989",
    "endTime": "03:59:59.988"
  }, {
    "count_id": 4,
    "avg_temperature": 2816,
    "startTime": "01:39:59.995",
    "endTime": "01:59:59.994"
  }, {
    "count_id": 5,
    "avg_temperature": 2668,
    "startTime": "04:19:59.987",
    "endTime": "04:39:59.986"
  }, {
    "count_id": 3,
    "avg_temperature": 2711,
    "startTime": "05:19:59.984",
    "endTime": "05:39:59.983"
  }, {
    "count_id": 9,
    "avg_temperature": 2697,
    "startTime": "03:59:59.988",
    "endTime": "04:19:59.987"
  }, {
    "count_id": 4,
    "avg_temperature": 2560,
    "startTime": "05:59:59.982",
    "endTime": "06:19:59.981"
  }, {
    "count_id": 4,
    "avg_temperature": 2837,
    "startTime": "03:19:59.99",
    "endTime": "03:39:59.989"
  }]

我想要此json对象的所有avg_temperature值的列表。

getHistoryData() {
    this.historyDataService.getHistoryData(this.payload)
      .subscribe((data : any) => this.response = data.dataPoints[0].avg_temperature)
  }

我正在以这种方式捕获我的响应,这给了我一个特定的值,但是我想要avg_temperature的所有值。如何遍历响应/获取所有avg_temperature值。同样,我想将所有其他参数(如startTime,endTime)存储在数组中,并在其他地方使用它们

2 个答案:

答案 0 :(得分:3)

您正在寻找 Array.map

  

map()方法使用调用a的结果创建一个新数组   在调用数组中的每个元素上提供了功能。

您的代码将是

getHistoryData() {
    this.historyDataService.getHistoryData(this.payload)
      .subscribe((data : any) =>
    this.response = data.dataPoints.map(a => a.avg_temperature)
}

答案 1 :(得分:-1)

您可以使用map

let obj ={"success":true,"dataPoints":[{"count_id":4,"avg_temperature":2817,"startTime":"00:00:00","endTime":"00:19:59.999"},{"count_id":4,"avg_temperature":2814,"startTime":"00:59:59.997","endTime":"01:19:59.996"},{"count_id":4,"avg_temperature":2816,"startTime":"00:39:59.998","endTime":"00:59:59.997"},{"count_id":4,"avg_temperature":2825,"startTime":"02:19:59.993","endTime":"02:39:59.992"},{"count_id":4,"avg_temperature":2828,"startTime":"02:39:59.992","endTime":"02:59:59.991"},{"count_id":4,"avg_temperature":2832,"startTime":"02:59:59.991","endTime":"03:19:59.99"},{"count_id":4,"avg_temperature":2841,"startTime":"03:39:59.989","endTime":"03:59:59.988"},{"count_id":4,"avg_temperature":2816,"startTime":"01:39:59.995","endTime":"01:59:59.994"},{"count_id":5,"avg_temperature":2668,"startTime":"04:19:59.987","endTime":"04:39:59.986"},{"count_id":3,"avg_temperature":2711,"startTime":"05:19:59.984","endTime":"05:39:59.983"},{"count_id":9,"avg_temperature":2697,"startTime":"03:59:59.988","endTime":"04:19:59.987"},{"count_id":4,"avg_temperature":2560,"startTime":"05:59:59.982","endTime":"06:19:59.981"},{"count_id":4,"avg_temperature":2837,"startTime":"03:19:59.99","endTime":"03:39:59.989"}]}

let op = obj.dataPoints.map( ({avg_temperature}) => avg_temperature )

console.log(op)