當我運行非常簡單的下面的代碼時,出於某種原因,我在瀏覽器控制檯中得到以下結果:「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