forEach
方法循环数据,因为cat照片对象保存在数组中。当您到达每个项目时,您可以修改HTML元素。首先,使用var html = "";
声明一个html变量var html = "";
。然后,遍历JSON,将HTML添加到包含strong
标记中的键名的变量,然后是值。循环结束后,渲染它。这是执行此操作的代码: json.forEach(function(val){
var keys = Object.keys(val);
html + =“<div class ='cat'>”;
keys.forEach(function(key){
html + =“<strong>”+ key +“</ strong>:”+ val [key] +“<br>”;
});
html + =“</ div> <br>”;
});
forEach
方法以循环JSON数据并创建HTML元素以显示它。这是一些JSON示例[
{
“ID”:0,
“IMAGELINK”: “https://s3.amazonaws.com/freecodecamp/funny-cat.jpg”
“altText”:“头上戴着绿色头盔形状瓜的白猫。”,
“codeNames”:[“Juggernaut”,“华莱士夫人”,“毛茛”
]
}
]
html
变量中
testString: 'assert(code.match(/html\s+?(\+=|=\shtml\s\+)/g), "Your code should store the data in the html
variable");'
- text: 您的代码应该使用forEach
方法来循环API中的JSON数据。
testString: 'assert(code.match(/json\.forEach/g), "Your code should use a forEach
method to loop over the JSON data from the API.");'
- text: 您的代码应将密钥名称包装在strong
标记中。
testString: 'assert(code.match(/.+<\/strong>/g), "Your code should wrap the key names in strong
tags.");'
```
```