gpt4 book ai didi

javascript - 使用 for 循环遍历数组和 console.log 中的对象,看是否看过电影

转载 作者:行者123 更新时间:2023-11-29 10:58:26 24 4
gpt4 key购买 nike

我刚开始学习编码。使用 for 循环和 forEach 有点困难。我的代码由于某种原因无法正常工作,而且我不知道错误是什么。有人看到我需要修复的任何东西吗?


var myMovies = [

{name: "Gladiator",
rating: 5,
seen: true
},
{name: "Vanilla Sky",
rating: 4.5,
seen: true
},
{name: "Beautiful Mind",
rating: 5,
seen:true
},
{name: "The Happytime Murders",
rating: 3.5,
seen: false
}
];
var movies = myMovies;
for(i = 0; i < 3; i ++)
if(movies.seen){
console.log("You have seen " + movies.name + "-" + movies.rating);
}else
console.log("You haven't seen " + movies.name + "-" + movies.rating);

最佳答案

你的问题是你的 myMoviesmovies 变量是数组,你需要显示你想要的索引。这是您的固定代码:

var myMovies = [

{name: "Gladiator",
rating: 5,
seen: true
},
{name: "Vanilla Sky",
rating: 4.5,
seen: true
},
{name: "Beautiful Mind",
rating: 5,
seen:true
},
{name: "The Happytime Murders",
rating: 3.5,
seen: false
}
];
var movies = myMovies;
for(i = 0; i < movies.length; i++) {
if(movies[i].seen){
console.log("You have seen " + movies[i].name + "-" + movies[i].rating);
}else
console.log("You haven't seen " + movies[i].name + "-" + movies[i].rating);

关于javascript - 使用 for 循环遍历数组和 console.log 中的对象,看是否看过电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52124332/

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