简体   繁体   English

Javascript:迭代嵌套在 object 中的对象数组作为值

[英]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.我对使用 Javascript 中的 API 相当陌生,并且我正在开发一个使用测验问题生成器 API 的测验应用程序项目。 I have figured out how to display the API response in the console to view the Javascript object?我已经弄清楚如何在控制台中显示 API 响应以查看 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. (我在响应中使用了 typeof ,它告诉我它是一个字符串),现在我试图弄清楚如何从结果的值中推断出问题、正确答案和错误答案(也是一个数组)等信息似乎是一个对象数组。 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.首先,您需要将其转换为 JSON Object 以便您可以在数据中使用它,此 function 的第一行就是为您做的。 if you log the type of this on the console you will see object not string.如果您在控制台上记录此类型,您将看到 object 不是字符串。 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.感谢您提出这个问题,我喜欢它,因为它在学习如何编码时给我带来了很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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