gpt4 book ai didi

javascript - foreach 循环中的未定义值

转载 作者:太空宇宙 更新时间:2023-11-04 15:39:26 26 4
gpt4 key购买 nike

有人可以解释为什么我的循环中未定义吗?我正在尝试运行一个循环,让我知道如果我观看了某些内容以及评级,它是否属实。我做错了什么?

var films = [

{
movie: "fast and the furious",
rating: 4,
seen: true
},
{
movie:"13 reasons why",
rating: 5,
seen: true


},
{
movie:"beaty and the beast",
rating: 4,
seen: false
}

];

films.forEach(function(movie){
var result = "You have ";
if(movie.seen) {
result += "watched ";
}
else{
result += "not seen ";
}
result += "\"" + films.movie + "\" - ";
result += films.rating + " stars";

console.log(result);

});

最佳答案

您应该在迭代器函数中访问元素,而不是数组:

films.forEach(function(movie) {
var result = "You have ";
if (movie.seen) {
result += "watched ";
} else {
result += "not seen ";
}
result += "\"" + movie.movie + "\" - ";
result += movie.rating + " stars";
});

关于javascript - foreach 循环中的未定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44016210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com