Reputation: 3
I'm working on learning functional programming, and have been seeing a lot of arrow functions. The arrow functions I'm looking at are accessing arrays and objects and I'm trying to understand why the parameters and statements are singular versions of the array/object name while the actual name is plural? I'm adding a sample to show what I mean:
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const names = users.map(user => user.name);
console.log(names); // [ 'John', 'Amy', 'camperCat' ]
Upvotes: 0
Views: 76
Reputation: 8091
You have an array of users
, that is to say, a list of users. Every element in the array is a user.
So as others have already pointed out, it's just a convention.
Really smart IDE's will even autogenerate the singular name from the plural when you use code hints/ auto generation.
Upvotes: 1
Reputation: 715
The map function takes a callback, that user is just one object of the array, it is the same as
for(user of users){}
The map function is what you are really looking at, not arrow functions. This caused me some confusion when I first leared about map()
, but really it's all just style.
Upvotes: 0