简体   繁体   中英

JS only picking up the first set in an array

So I have a array declared as:

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,
  },
];

And I'm trying to access each movie with:

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

but for some reason it only recognize the first data (Deadpool 2) and say the reset of the data is undefined. When I delete the first data (Deadpool 2) and let the second data become the first (Avengers), it recognized that data but says the rest is undefined.

What's going on?

PS. I'm working with MongoDB, but that shouldn't matter....

Take a look at this page to find your answer.

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

You line

movieList.forEach(function (movie)

should be

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

Try using for of . It works for me: http://jsfiddle.net/9vyufz5h/

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

Not sure what happened but it fixed on it's on.. went back to using underscore's _.each()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM