2

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.

2
  • 2
    Please post text, not links to images of text. But yes, there is. Commented May 1, 2020 at 16:16
  • Since I just joined the platform, it said that I need to earn 10 reputation before I can do that. Commented May 1, 2020 at 16:20

2 Answers 2

3

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%;}

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! thank you. But will you be kind enough to explain it a bit as to my understanding map returns a new array of key:value pairs and the flat() method just flattens the array so why didn't we have a single array with all the elements as keys or values rather than just values?
oh okay. Thank you so much. Also marked the answer :)
0

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'])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.