var movies = [
{
title: "Silence of the Lambs",
rating: "5 stars",
hasWatched: true
},
{
title: "Frozen",
rating: "4 stars",
hasWatched: false
},
{
title: "Inception",
rating: "1 stars",
hasWatched: true
}
]
for (i=0; i<movies.length; i++){
var result = "You have "
if (movies[i].hasWatched=true) {
result += "seen ";
} else {
result += "not seen ";
}
console.log(result + movies[i].title + " - " + movies[i].rating)
}
我試圖打印「你已經看過這部電影等」如果hasWatched = true並且「你還沒有看過這部電影等」如果hasWatched = false。問題是,在這個版本中,它總是打印「看到」,即使是「凍結」。我試圖在if語句中加入「movies [i] .hasWatched = false」,在這種情況下,它總是打印出未看到的。可能是什麼問題呢?不能做出正確的如果陳述與對象相關
編輯:感謝球員這是一個非常愚蠢的錯誤。 Ty大家。
'='應該是''==或更好的''===重複 – Andreas
https://stackoverflow.com/questions/13851399/javascript-if-statements-not-working。 – 2017-05-30 06:37:37
任何棉絨都會挑選它。 – 2017-05-30 06:40:24