简体   繁体   中英

Javascript: Iterate over an array of objects nested within an object as a value

I am fairly new to working with APIs in Javascript and I am working on a quiz app project that makes use of a quiz question generator API. I have figured out how to display the API response in the console to view the Javascript object? string? (I have used typeof on the response which tells me it is a string), and now I am trying to figure out how to extrapolate information such as questions, correct answer, and incorrect answer(also an array) from the value for result that seems to be an array of objects. Any help regarding this issue would be greatly appreciated!

 var request = new XMLHttpRequest();

request.open('Get', 'MyAPIKey', true);

request.onload = function() {
 console.log(request.status);
 console.log(request.response);
};

在此处输入图像描述

Blockquote

在此处输入图像描述

Blockquote在此处输入图像描述

request.onload = function() { 
   let res = JSON.parse(request.response);

   console.log(typeof res);
   console.log(res);
};

first you need to convert this to a JSON Object so that you can be able to work with it in your data, the very first line on this function is doing that for you. if you log the type of this on the console you will see object not string. I hope this help. thanks for asking this question, i like it because it gave me a lot of trouble while learning how to code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM