Let movies = [] Output wrong

I don’t know why this don’t work, because I saw the result need to be a array:

ratings should equal [{"title": "Inception", "rating": "8.8"}, {"title": "Interstellar", "rating": "8.6"}, {"title": "The Dark Knight", "rating": "9.0"},{"title": "Batman Begins", "rating": "8.3"}, {"title": "Avatar", "rating": "7.9"}]

so I just:

let movies = [];

but seems it doesn’t work :

const ratings = watchList.map(movie => { 
  let movies = [];
  movies["title"] = movie["Title"];
  movies["rating"] = movie["imdbRating"]
  return movies;
});

Output

[[],[],[],[],[]]

but when I change for this:

const ratings = watchList.map(movie => { 
  let movies = {};
  movies["title"] = movie["Title"];
  movies["rating"] = movie["imdbRating"]
  return movies;
});

It passed!
I dont’t get it…

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Use the map Method to Extract Data from an Array

Link to the challenge:

[] is an array, {} is an object. The expected result is array of objects, not array of arrays.

1 Like

The other thing to note is that you got an bunch of empty arrays is because the way you where adding the values to an array was incorrect. You technically did add them to the movies array object, but not in the way arrays are intended to be used with indexes.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.