我发现代码按我的预期工作(我认为)。但是我打印数据的最后一步感觉不对。我连接 html 和 js 的方式似乎有点不对劲。有没有更好的方法来连接它?我是否使用了错误的解决方案来打印数据?
// 这个列表我在我的数组中使用。
const myList = {
Germany : {name : 'Germany', capital: 'Berlin', visited: 'Checked' },
Italy : {name : 'Italy', capital: 'Rome', visited: 'Checked' },
Spain : {name : 'Spain', capital: 'Madrid', visited: 'unchecked' },
}
// 我的数组
const destinations = [];
// 将数据从 myList 推送到目标数组。
for(var key in myList) {
destinations.push(myList[key]);
}
// 这就是在页面上写出我的数据的方式。
for (var i = 0; i < destinations.length; i++) {
document.write("<li><h1>" + destinations[i].name + "</h1><p>" +
destinations[i].capital +
"<input type='checkbox'" + destinations[i].visited + ">")
};
这就是我打算在最后写出来的。
<li class="all-destinations">
<h3>destinations[i].name</h3>
<div class="container">
<label class="switch">
<input type="checkbox" destinations[i].visited>
</label>
</div>
<p>destinations[i].capital</p>
<hr>
</li>