.map function javascript

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

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

what does the “user” mean inside the map function ?

It is a function parameter name describing what input the arrow function is expecting (and will get with this map)

1 Like

So we dont need to decare it first ?

and what if i want both the name and age by using map

I think you might be confusing arrow function syntax. I’d review that.

1 Like

okay got its the function’s parameter but lets suppose the users had name age and address … what if i want to have an array with one key as name and other as age … how will I write the code for that.

Arrays don’t have keys.

The map is being called on an array of objects, so the first argument to the callback function is the individual array elements, the individual user objects

1 Like

Thank You so much for spending your precious time and explaining all this … If possible can you explain me the following code as well …

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

How might we determine what this code does?

const ratings = watchList.map(item => {
  console.log("in the callback with", item);
  return {
    title: item["Title"],
    rating: item["imdbRating"]
1 Like

so as the map fucntion iterates over it is taking every element of watchList and putting it in place of the item argument and returning its Title and imdbRating.

Is my explanation correct ?

1 Like

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