Declaring Objects on the fly with .map()

Tell us what’s happening:

Why is it the case that this works:

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

but this doesn’t:

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

In the one that doesn’t work we’re declaring an Object literal to be added to the array that map will return? What makes the parentheses wrapping an Object literal different?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0.

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

Link to the challenge:

Parentheses allows here for implicit return. Otherwise brackets will be considered as a opening and closing of function body, what would require a traditional return statement.

1 Like