所以我有一个声明为的数组:
const movieList = [
{
Title: 'Deadpool 2',
Year: '2018',
Rated: 'R',
Released: '18 May 2018',
Runtime: '119 min',
Genre: 'Action, Adventure, Comedy, Sci-Fi',
Director: 'David Leitch',
Writer: 'Rhett Reese, Paul Wernick, Ryan Reynolds',
Actors: 'Ryan Reynolds, Josh Brolin, Morena Baccarin, Julian Dennison',
Plot: 'Foul-mouthed mutant mercenary Wade Wilson (AKA. Deadpool), brings together a team of fellow mutant rogues to protect a young boy with supernatural abilities from the brutal, time-traveling cyborg, Cable.',
Language: 'English',
Poster: 'https://m.media-amazon.com/images/M/MV5BNjk1Njk3YjctMmMyYS00Y2I4LThhMzktN2U0MTMyZTFlYWQ5XkEyXkFqcGdeQXVyODM2ODEzMDA@._V1_SX300.jpg',
HomePoster: 'https://m.media-amazon.com/images/M/MV5BNjk1Njk3YjctMmMyYS00Y2I4LThhMzktN2U0MTMyZTFlYWQ5XkEyXkFqcGdeQXVyODM2ODEzMDA@._V1_SX300.jpg',
imdbRating: '7.8',
Quantity: 100,
},
{
Title: 'Avengers: Infinity War',
Year: '2018',
Rated: 'PG-13',
Released: '27 Apr 2018',
Runtime: '149 min',
Genre: 'Action, Adventure, Fantasy, Sci-Fi',
Director: 'Anthony Russo, Joe Russo',
Writer: 'Christopher Markus (screenplay by), Stephen McFeely (screenplay by), Stan Lee (based on the Marvel comics by), Jack Kirby (based on the Marvel comics by), Joe Simon (Captain America created by), Jack Kirby (Captain America created by), Steve Englehart (Star-Lord created by), Steve Gan (Star-Lord created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Jim Starlin (Thanos, Gamora and Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Steve Englehart (Mantis created by), Don Heck (Mantis created by)',
Actors: 'Robert Downey Jr., Chris Hemsworth, Mark Ruffalo, Chris Evans',
Plot: 'The Avengers and their allies must be willing to sacrifice all in an attempt to defeat the powerful Thanos before his blitz of devastation and ruin puts an end to the universe.',
Language: 'English',
Poster: 'https://m.media-amazon.com/images/M/MV5BMjMxNjY2MDU1OV5BMl5BanBnXkFtZTgwNzY1MTUwNTM@._V1_SX300.jpg',
HomePoster: 'https://m.media-amazon.com/images/M/MV5BMjMxNjY2MDU1OV5BMl5BanBnXkFtZTgwNzY1MTUwNTM@._V1_SX300.jpg',
imdbRating: '8.5',
Quantity: 100,
}, {
Title: 'The Cloverfield Paradox',
Year: '2018',
Rated: 'TV-MA',
Released: '04 Feb 2018',
Runtime: '102 min',
Genre: 'Horror, Mystery, Sci-Fi, Thriller',
Director: 'Julius Onah',
Writer: 'Oren Uziel (screenplay by), Oren Uziel (story by), Doug Jung (story by)',
Actors: 'Gugu Mbatha-Raw, David Oyelowo, Daniel Brühl, John Ortiz',
Plot: 'Orbiting a planet on the brink of war, scientists test a device to solve an energy crisis, and end up face-to-face with a dark alternate reality.',
Language: 'English, Mandarin',
Poster: 'https://m.media-amazon.com/images/M/MV5BMTAwOTIxMDA0MjZeQTJeQWpwZ15BbWU4MDg1MjgzNzQz._V1_SX300.jpg',
HomePoster: 'https://m.media-amazon.com/images/M/MV5BMTAwOTIxMDA0MjZeQTJeQWpwZ15BbWU4MDg1MjgzNzQz._V1_SX300.jpg',
imdbRating: '5.6',
Quantity: 100,
},
];
我正尝试通过以下方式访问每部电影:
for (let movie in movieList) {
log.info(movie.Title); //this case im just trying to get a title
}
//and
movieList.forEach(function (movie) {
log.info(JSON.stringify(movie));
});
//and
for (let i = 0; i < movieList.length; i++) {
log.info(JSON.stringify(movieList[i]));
}
,但是由于某种原因,它只能识别第一个数据(死池2),并说该数据的重置未定义。当我删除第一个数据(Deadpool 2)并让第二个数据成为第一个数据(Avengers)时,它识别出该数据,但说其余数据未定义。
这是怎么回事?
PS。我正在使用MongoDB,但这没关系。...
答案 0 :(得分:0)
在此页面上查找您的答案。
https://docs.mongodb.com/manual/reference/method/cursor.forEach/
您排队
movieList.forEach(function (movie)
应该是
movieList.find().forEach(function (movie)
答案 1 :(得分:0)
尝试使用for of
。它对我有用:http://jsfiddle.net/9vyufz5h/
for (let movie of movieList) {
console.log(movie.Title)
}
答案 2 :(得分:0)
不知道发生了什么,但它已经固定了。.回到使用下划线的_.each()
_.each(movieList, function seedMovies(movie) {
log.info(movie.Title);
Movie.insert(movie);
});