JS只获取数组中的第一个集合



所以我有一个数组声明为:

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(并说数据的重置是未定义的。当我删除第一个数据(《死侍2》(,让第二个数据成为第一个(《复仇者联盟》(时,它识别出了那个数据,但说其余的数据没有定义。

怎么回事?

PS。我正在使用MongoDB,但这不重要。。。。

看看这个页面可以找到答案。

https://docs.mongodb.com/manual/reference/method/cursor.forEach/

你的

movieList.forEach(function (movie)

应该是

movieList.find().forEach(function (movie)

尝试使用for of。它对我有效:http://jsfiddle.net/9vyufz5h/

for (let movie of movieList) {
console.log(movie.Title)
}

不确定发生了什么,但它已经解决了问题。返回使用下划线的_.each((

_.each(movieList, function seedMovies(movie) {
log.info(movie.Title);
Movie.insert(movie);
});

相关内容

最新更新