Sarthak Kundra
Sarthak Kundra

Reputation: 95

how to convert an object of objects into separate values.?

I am trying to access information from a JSON API call to store the data into an array and use it further. This is an example API call example API . I am trying to access the "iaqi" proberty which is an object of objects. I need the integer values from it like 9,53,10,102... etc. I wrote the following code to convert it into an array of objects named as dataArray and then further iterated over it to get seperate key: value pairs.

 const dataArray = Object.values(response.body.data.iaqi);
        const iterator = dataArray.entries();

        for(const[index, element] of iterator){
            console.log(element)
        }

which gave me the following output :-

Output

Is there any way I can access the individual number values and store them in an array? Any help will be appreciated and please correct me if I did something wrong as I'm new here.

Upvotes: 2

Views: 104

Answers (2)

Yevhen Horbunkov
Yevhen Horbunkov

Reputation: 15540

You mean, like that?

const src = {"status":"ok","data":{"aqi":137,"idx":1451,"attributions":[{"url":"http://www.bjmemc.com.cn/","name":"Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心)"},{"url":"https://china.usembassy-china.org.cn/embassy-consulates/beijing/air-quality-monitor/","name":"U.S Embassy Beijing Air Quality Monitor (美国驻北京大使馆空气质量监测)"},{"url":"https://waqi.info/","name":"World Air Quality Index Project"}],"city":{"geo":[39.954592,116.468117],"name":"Beijing (北京)","url":"https://aqicn.org/city/beijing"},"dominentpol":"pm25","iaqi":{"co":{"v":5.5},"h":{"v":54},"no2":{"v":13.3},"o3":{"v":57.1},"p":{"v":1002},"pm10":{"v":80},"pm25":{"v":137},"so2":{"v":2.1},"t":{"v":27},"w":{"v":3.6}},"time":{"s":"2020-05-02 00:00:00","tz":"+08:00","v":1588377600},"debug":{"sync":"2020-05-02T01:17:30+09:00"}}},

      result = Object
        .values(src.data.iaqi)
        .map(Object.values)
        .flat()
        
console.log(result)
.as-console-wrapper{min-height:100%;}

If your inner objects have static structure (single v key), you may somewhat shorten the above

const src = {"status":"ok","data":{"aqi":137,"idx":1451,"attributions":[{"url":"http://www.bjmemc.com.cn/","name":"Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心)"},{"url":"https://china.usembassy-china.org.cn/embassy-consulates/beijing/air-quality-monitor/","name":"U.S Embassy Beijing Air Quality Monitor (美国驻北京大使馆空气质量监测)"},{"url":"https://waqi.info/","name":"World Air Quality Index Project"}],"city":{"geo":[39.954592,116.468117],"name":"Beijing (北京)","url":"https://aqicn.org/city/beijing"},"dominentpol":"pm25","iaqi":{"co":{"v":5.5},"h":{"v":54},"no2":{"v":13.3},"o3":{"v":57.1},"p":{"v":1002},"pm10":{"v":80},"pm25":{"v":137},"so2":{"v":2.1},"t":{"v":27},"w":{"v":3.6}},"time":{"s":"2020-05-02 00:00:00","tz":"+08:00","v":1588377600},"debug":{"sync":"2020-05-02T01:17:30+09:00"}}},

      result = Object
        .values(src.data.iaqi)
        .map(({v}) => v)
        
console.log(result)
.as-console-wrapper{min-height:100%;}

Upvotes: 3

Santosh
Santosh

Reputation: 2333

I made a work around using for...in. Looping over all the object entries and printing out the value of v.

var dataArray = {
  co: {
  v: 4.6
  },
  h: {
  v: 54
  },
  no2: {
  v: 9.2
  },
  o3: {
  v: 46.3
  },
  p: {
  v: 1002
  },
  pm10: {
  v: 70
  },
  pm25: {
  v: 137
  },
  so2: {
  v: 1.6
  },
  t: {
  v: 26
  },
  w: {
  v: 3.6
  }
}
for(const element in dataArray){
    console.log(dataArray[element]['v'])
}

Upvotes: 0

Related Questions