Map method extract data challenge

can somebody expert help me understanding this solution provided :

const ratings = watchList.map(({ Title: title, imdbRating: rating }) => ({title, rating}));

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

Link to the challenge:

It’s a lot of shorthand, but it’s pretty much equivalent to this:

watchList.map((movie) => {
  const title = movie.Title;
  const rating = movie.imdbRating;

  return {
    title: title,
    rating: rating
  }
});

({ Title: title, imdbRating: rating }) this bit is saying “put the Title property from the object in a variable called title, also take the imdbRating property and call it rating.”

=> ({title, rating}) “return an object with a property title set to the title variable from above, and likewise with rating.”

1 Like

Can you be more specific about what is confusing you? There is a lot going on there.

and why do we need to put the parentheses before the curly braces ?

i dont understand why the parentheses appear before the curly braces .

Yeah, that confuses a lot of people. It is because without it, JS will assume that it is a code block and not an implicit return of an object literal.

There is some discussion here. Take a look at that and check back if that doesn’t clear it up.

1 Like