Object syntax confusion

I do not understand why these objects work with the s removed.
“users” “user”

are they two different objects? is one a function?

I don’t understand how they relate to each other.
I don’t understand why the “user” isn’t “users”

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

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

Method to Extract Data from an Array

Link to the challenge:

1 Like

user is just the name of a variable. Map is saying “loop over the array users and run a function for each item in it”. The function takes an argument, the author has called that argument user because each item represents a single user

(user) => user.name

is a function, is same as something like

function getName (user) {
  return user.name;
}

It’s a function that takes an object and returns the value of the property with the key of name

2 Likes

Array.map takes a function as first argument.

That callback function is called for each element of the array, and takes the current element being processed as argument.

Meaning that what’s inside is “just” a function, and like with every function you can call the arguments whatever you like.

So this would have been fine, even tho we can argue holds less meaning for a reader:

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

Hope this helps :sparkles:

3 Likes

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