作爲Udemy Web開發培訓營的代碼的一部分(由Colt Steele提供),我有以下JavaScript,它簡單地列出了數組中的項,更改console.log文本取決於布爾型「hasWatched」條件。現在,console.log返回所有數組項,就像它們是真實的一樣。如果條件在數組迭代中總是返回true
// Create an array of objects. Each movie should have a title, rating and hasWatched properties. Iterate through the array and print out results and whether it has been hasWatched
var movieArr = [
{
title: "LOTR",
rating: 5,
hasWatched: true
},
{
title: "Fast and the Furious",
hasWatched: false,
rating: 1
},
{
title: "Let the Right One In",
rating: 5,
hasWatched: false
}
]
for(i = 0; i < movieArr.length; i++){
if(movieArr[i].hasWatched = true){
console.log("You have seen " + movieArr[i].title + ": Rating: " + movieArr[i].rating);
} else {
console.log("You have not seen " + movieArr[i].title + ": Rating: " + movieArr[i].rating);
}
}
我在這裏錯過了什麼?
非常感謝! 裏克
嘛看起來你並沒有複製所有'movieArr [I] .hasWatched = TRUE; – epascarello
*如果(movieArr [I] .hasWatched ==真){* theres區分*賦值*和*比較* –
...或只是'如果(movieArr [i] .hasWatched)'。 –