--- id: 587d7fae367417b2b2512be5 title: Convert JSON Data to HTML challengeType: 6 videoUrl: '' localeTitle: 将JSON数据转换为HTML --- ## Description
现在您正在从JSON API获取数据,您可以在HTML中显示它。您可以使用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>”;
});
## Instructions
添加forEach方法以循环JSON数据并创建HTML元素以显示它。这是一些JSON示例
[
{
“ID”:0,
“IMAGELINK”: “https://s3.amazonaws.com/freecodecamp/funny-cat.jpg”
“altText”:“头上戴着绿色头盔形状瓜的白猫。”,
“codeNames”:[“Juggernaut”,“华莱士夫人”,“毛茛”
]
}
]
## Tests
```yml tests: - text: 您的代码应该将数据存储在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.");' ```
## Challenge Seed
```html

Cat Photo Finder

The message will go here

```
## Solution
```js // solution required ```