-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.

0

2 Answers 2

-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.

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

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.