-1

I have this code that filters an array using the .filter method. I'm extremely confused about the function's parameters that is being specified in the .filter method.

Where does the parameter come from? How do I know when to add a parameter like 'value', and what is the value of the parameter 'value'?

var newArray = [1,2,3,4,5,6,7,8,9,10];

    newArray = newArray.filter(function(value) {
      return value < 6;
    });

I'm not too sure if it is the right term to use.

Kai Chen
  • 3
  • 2

2 Answers2

-1

Where does the parameter come from?

It gets passed to the function when the function is called … which will happen somewhere inside filter or a function called by filter.

How do I know when to add a parameter like 'value',

Generally by reading the documentation

what is the value of the parameter 'value'

From the documentation, the first argument is:

element: The current element being processed in the array.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Array is a JavaScript class and each JavaScript class can have prototype. Filter method is a part of Array prototype and this function is getting as an argument callback function with one argument.

Inside Filter function, your callback is being called on each array item.

You can find more informations about this function in JavaScript docs.

Mateusz Woźniak
  • 1,479
  • 1
  • 9
  • 10