1

I have a json of type

{
  "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"
  }]

I want the list of all the avg_temperature values from this json object.

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

I am capturing my response this way which gives me one particular value , but i want all the values of avg_temperature. How do I loop through the response/ get all the avg_temperature values. Similarlly I want to store all the others parameters like startTime , endTime in an array and use them in some other place

chink
  • 1,505
  • 3
  • 28
  • 70
  • the duplicate question answers how to get a list of all values of single parameter, i want to get list of all values of all the parameters present in each json object – chink Apr 13 '19 at 06:08

2 Answers2

2

You are looking for Array.map.

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

Your code will be,

getHistoryData() {
    this.historyDataService.getHistoryData(this.payload)
      .subscribe((data : any) =>
    this.response = data.dataPoints.map(a => a.avg_temperature)
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 1
    this way `response` object contains all the avg_temperature values, what if I want others values like `count_id , startTime , endTime` also in an array – chink Apr 13 '19 at 05:49
  • 1
    I am trying to make a call to backend for each parameter, Is it a right way to do because now I am calling backend 4 times and recieveing the same data every time? – chink Apr 13 '19 at 06:55
  • 1
    No you should create an interface of the response type and map the necessary fields – Sajeetharan Apr 13 '19 at 07:07
  • 1
    https://stackoverflow.com/questions/46455573/angular2-typescript-map-response-to-new-type – Sajeetharan Apr 13 '19 at 07:08
-1

You can use 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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60