Javascript对象返回undefined

时间:2015-11-24 19:58:08

标签: javascript

var movies = 
[
{name: "In Bruges", rating:"4.7", seen:false},
{name: "Frozen", rating: "4.5", seen:true},
{name: "Lion King", rating:"5", seen:true},
]


for (var i = 0; i < movies.length; i++) {
    var result = "You have ";
    if(movies.seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result += "\"" + movies.name + "\" - ";
    result += movies.rating + " stars"
    console.log(result)
};

You have not seen "undefined" - undefined stars is the result in chrome however you should see You have seen/not seen "movie name" - "rating" stars.

我需要使用for循环打印出我观看过的每部电影和评级,以及我是否看过它。问题是为什么它未定义?代码应该看电影吗?只是替换那里的值?有人可以查看我的代码并帮助我进行for循环吗?

6 个答案:

答案 0 :(得分:3)

movies是一个数组movies.seen将无法使用movies[i].seen并且与其他属性一样明智

答案 1 :(得分:1)

movies.seenmovies.ratingmovies.name更改为movies[i].seenmovies[i].ratingmovies[i].name

答案 2 :(得分:1)

使用索引访问数组项;例如movies[i]

for (var i = 0; i < movies.length; i++) {
    var result = "You have ";
    if(movies[i].seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result += "\"" + movies[i].name + "\" - ";
    result += movies[i].rating + " stars"
    console.log(result)
}

或者您可以将数组项存储在变量中并使用它:

for (var i = 0; i < movies.length; i++) {
    var movie = movies[i];
    var result = "You have ";
    if (movie.seen) {
        result += "watched ";
    }
    else {
        result += "not seen ";
    }
    result += '"' + movie.name + '" - ';
    result += movie.rating + " stars";
    console.log(result);
}

答案 3 :(得分:0)

Here is a working Plunker让你看到它有效。 :)

var movies = 
[
{name: "In Bruges", rating:"4.7", seen:false},
{name: "Frozen", rating: "4.5", seen:true},
{name: "Lion King", rating:"5", seen:true},
]


for (var i = 0; i < movies.length; i++) {
    var result = "You have ";
    if(movies[i].seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result += "\"" + movies[i].name + "\" - ";
    result += movies[i].rating + " stars"
    alert(result)
}

您试图访问名为seemnamerating的电影的属性,但电影有3个对象从0到2索引。

答案 4 :(得分:0)

您在循环内使用电影,电影不包含这些属性。 并且在迭代数组时使用forE循环总是安全的

执行类似

的操作
movies.forEach(function(movie){
var result = "You have ";
    if(movie.seen === true){
        result += "watched ";
    }
    else{
        result += "not seen ";
    }
    result +=  movie.name + " - ";
    result += movie.rating + " stars";
    console.log(result);

});

答案 5 :(得分:0)

我建议使用一些其他方法来迭代数组并检查是否已经看过电影。

&#13;
&#13;
var movies = [
    { name: "In Bruges", rating: "4.7", seen: false },
    { name: "Frozen", rating: "4.5", seen: true },
    { name: "Lion King", rating: "5", seen: true },
];

movies.forEach(function (movie) {
    var result = "You have ";            
    result += movie.seen ? "watched " : "not seen ";            
    result += "\"" + movie.name + "\" - ";
    result += movie.rating + " stars"
    document.write(result + '<br>');
});
&#13;
&#13;
&#13;