【发布时间】:2019-12-07 06:36:00
【问题描述】:
真的很基础,我知道,但我只是进入 Javascript 并练习一些数组和对象。我已经编写了一个基本的电影数据库,其中存储在 var movies 中的对象,但是控制台一直显示字符串 ive build 它应该在其中插入 movie.title 为 undefined。我错过了什么?
var movies = [
{
"title": "Armagedon",
"rating": 4.8,
"hasWatched": true,
},
{
"title": "Tomb Raider",
"rating": 2.6,
"hasWatched": true,
},
{
"title": "Fight Club",
"rating": 4.9,
"hasWatched": true,
}
];
movies.forEach(function(movie) {
var item = "You have ";
if (movies.hasWatched = true) {
item += "watched ";
} else {
item += "not seen ";
}
item += "\"" + movies.title + "\" - ";
item += movies.rating + " stars";
console.log(item);
})
这显然是一些非常基本的东西,所以放轻松!谢谢!
【问题讨论】:
-
你应该使用
movie.title,而不是movies.title。其他参考文献也一样。 -
您使用了
movies而不是movie(当前项目)。
标签: javascript arrays javascript-objects