我仍在学习javascript,需要一点帮助。 我得到的是undefined而不是我自己的字符串,有人可以告诉我我的代码有什么问题。预先感谢。
我的代码:
const logoData = [
{
name: "Arizona Cardinals",
sport: "Football"
},
{
name: "Atlanta Falcons",
sport: "Football"
}
];
document.getElementById("library").innerHTML = `
${logoData.map(function(logo) {
return `
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">${logoData.name}</h5>
</div>
</div>
`
}).join('')}`
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<!--Bootstrap style-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!--Bootstrap scripts-->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<div id="library"></div>
</body>
答案 0 :(得分:-1)
在map
回调中,您要使用的变量称为logo
:
<h5 class="card-title">${logo.name}</h5>