0

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' ]

5
  • 2
    Just naming conventions. Nothing special about it Commented Jun 3, 2020 at 22:32
  • This has nothing to do with arrow functions, it's about functions like map() that iterate over arrays. Commented Jun 3, 2020 at 22:33
  • 1
    It just makes sense. If you have an array of things, the name of the thing would be plural to signify what it is. And when you map over them, a single element of the array will be passed in for each iteration, so it makes sense to name them singularly, because that's what they are Commented Jun 3, 2020 at 22:33
  • 1
    You would use the same naming with traditional functions: users.map(function(user) ...) Commented Jun 3, 2020 at 22:34
  • 1
    jsfiddle.net/61a7fs8c Variable names don't really mean anything to the engine. They are just identifiers. Any meaning applied to them by developers are there to provide clarity and self documentation to the process. Commented Jun 3, 2020 at 22:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.