簡體   English   中英

JS只拾取數組中的第一組

[英]JS only picking up the first set in an array

所以我有一個數組聲明為:

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,但這沒關系。

查看此頁面以找到您的答案。

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);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM