Help with filter arguments

Hi everyone, the JavaScript classes are getting more and more complicated for me, and now I had a question about the “filter” arguments, namely the first one. See in my screenshot: where was “user” defined? I assume this is the name for a full array in "
users"? But I don’t see a definition for “user” anywhere. It may be a stupid question, but I’m confused.

You are passing a function into the filter method. The code

user => user.age < 30

is a function definition using the arrow operator. So user is just the name of the function parameter. You get to decide what you want to name function parameters. So you could use any name you want here. But since you know you are applying this filter to the users array then it makes sense to use the name user.

Thank you very much for the answer! I thought it meant “equal or more”… But I still don’t understand where it gets the “user.age”… What I see is that user gets the definiton of “user.age < 30”, Why I am so stupid? Thanks anyway.

I rewrote the code with a bit verbose syntax, and additional console.log-s.
But it is still doing same thing.
Try to run it and look into stuff in the console.
Hope that helps with understanding.

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

const usersUnder30 = users.filter((user) => {
  console.log('user: ', user);
  console.log('user.age ', user.age);
  return user.age < 30;

The filter method passes each element in the users array into the function you provide. Each element in the users array is an object, each of which has an age property. The function you provided for the filter method is using user as the name for the object being passed into it (again, you can use whatever name you want here, you are defining the function, but user makes the most sense). And since we already know that each object passed into that function has an age property, then you can access that property with dot notation (users.age).

Thank you very much for your patience and support. I’m starting to think a little clearer…

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