forEach
方法来遍历它。当你拿到每个对象时,你就可以修改 HTML 元素了。
首先,用let html = "";
声明一个变量。
接着,遍历 JSON,将 HTML 添加到用strong
标记键名的变量,后面跟着值。当循环结束后渲染它。
这是执行此操作的代码:
```js
let html = "";
json.forEach(function(val) {
const keys = Object.keys(val);
html += "forEach
循环 JSON 数据的方法,并创建 HTML 元素以显示它。
以下是一个 JSON 示例:
```json
[
{
"id":0,
"imageLink":"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg",
"altText":"A white cat wearing a green helmet shaped melon on its head. ",
"codeNames":[ "Juggernaut", "Mrs. Wallace", "Buttercup"
]
}
]
```
html
变量中。
testString: assert(code.match(/html\s+?(\+=|=\shtml\s\+)/g));
- text: 你的代码应该使用forEach
方法来遍历 API 中的 JSON 数据。
testString: assert(code.match(/json\.forEach/g));
- text: 你的代码应该将键名包装在strong
标签中。
testString: assert(code.match(/.+<\/strong>/g));
```
```
```