当我运行非常简单的下面的代码时,出于某种原因,我在浏览器控制台中得到以下结果:“6您尚未观看undefined undefined。任何人都可以指出我的错误吗?'for'循环没有正确循环通过对象数组
var movies = [{
title: "The Mummy",
hasWatched: true,
stars: "5 stars"
},
{
title: "About A Boy",
hasWatched: true,
stars: "5 stars"
},
{
title: "It",
hasWatched: false,
stars: "5 stars"
},
{
title: "Cleopatra",
hasWatched: false,
stars: "5 stars"
}
];
for (var i = 0; i <= movies.length; i++) {
if (movies.hasWatched) {
console.log("You have watched " + movies.title + " " + movies.stars + ".");
} else {
console.log("You have not watched " + movies.title + " " + movies.stars + ".");
}
}
'movies'是一个数组。你需要用'i'来索引它。即'电影[i] .hasWatched' – jlars62