Map method difference

So, in the " Use the map Method to Extract Data from an Array" lesson we’re supposed to transform that for loop in a map method that will return the same thing.

So I did this:

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

but the lesson would not complete. So I look at the exercise resolution, which is:

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

I called the exercise resolution “ratings 2” so that I can compare both returns, and printed both variable results to the console:

console.log(JSON.stringify(ratings));
console.log("---")
console.log(JSON.stringify(ratings2));

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

It seems to me that both resolutions are returning exactly the same. So I wonder why my code didn’t work and the resolution did, could somebody point it out for me?

Thanks in advance!

There’s a typo in the second key name, it should be singular rating.

No, but see, I think I wasn’t clear enough: the second key name I just put on to compare both solutions, since I can’t log two different variables with the same name.

The one here :slight_smile:

return {title: film["Title"], ratings: film["imdbRating"]}
1 Like

Damn, now I feel ashamed, that’s precisely what it was!

Thanks a lot!

No need to feel bad about it. It can be hard to notice details like that, that don’t generate any obvious error.

1 Like