Functional Programming - Use the map Method to Extract Data from an Array

I don’t understand what goes into maps. I am pretty lost. Why is there an arrow, and is user a new variable? It would be a big help if someone could explain what to put into maps.

*Your code so far

const users = [
  { name: 'John', age: 34 },
  { name: 'Amy', age: 20 },
  { name: 'camperCat', age: 10 }
];

const names = users.map(user => user.name);
console.log(names);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

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

Link to the challenge:

The arrow is a different way to write a function

Map takes as its argument a function that accepts an array element as the input and returns the corresponding element for the new array.

Ok, I came up with this but it still doesn’t work. Wouldn’t map look at each object in the watchList and create a new object with title and ratings then put all the new objects in an array? Edit: the solution has 1 more parenthesis around the object. If I do that then it works. What is the need for the extra parenthesis?

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

You need to do something extra to return an object from an arrow function. I’m not at my desk, but I’d Google “arrow function return object mdn”

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