Use the map Method to Extract Data from an Array

Hi, so far this is the first challange I simply wasn’t able to do and frankly It took me a lot of time to even understand one of two solutions in “Hints”, but it still isn’t clear.

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

Above one I think I almost get. I will try to explain it and I’d like to ask you to correct me if I’m wrong:

  • it uses map method, and its argument is a callback function, which is going to be applied to every object in an array of objects watchList,
  • that callback function uses an arrow function syntax,
  • item is an arrow funcion argument, the funcion returns an object
({
  title: item["Title"],
  rating: item["imdbRating"]
})
  • title and rating are property names of the returned object, which means that they are actually strings (js syntax allows us to skip “” for object properties), thats why they are enclosed by quote marks,
  • item[“Title”] needs quotation marks because otherwise Title would be confused for some undefined variable named Title

One thing i really don’t understand is wy do I have to enclose the:

{
  title: item["Title"],
  rating: item["imdbRating"]
}

into parentheses like this for it to work:

({
  title: item["Title"],
  rating: item["imdbRating"]
})

This article explains - you can’t mix an implicit return with an object literal unless you use the ()


These functional programming challenges are tricky to wrap your head around. I recommend asking questions before looking up the solutions.

Ok, I get it, in arrow funcions after the “=>” compiler has to decide if I’m doing an implicit or explicit return. Since I want to implicitly return an object, which starts with “{” it mistakes my object for an explicit declaration.

Bingo. So the () wraps that in order to prevent that misunderstanding.