10
numbers = [1,2,3,4,5,4,3,2,1]; 
var filterResult = numbers.filter(function(i){
    return (i > 2);
});       

I don't understand how this works. if I omit the i as a function argument it breaks the function but the i isn't tied to anything so why does it need to be there?

5
  • 2
    Maybe this resource is helpful to you: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… Commented Jun 27, 2012 at 22:51
  • So the i is used by the filter function as a way to pass whatever the value at an index of numbers is? javascript goes and gets each element I just need to tell it what to do with i? Commented Jun 27, 2012 at 23:04
  • In simple terms: If you call filter() on an array, JavaScript takes each value of that array and calls the function that you specified (in this case function(i) { return (i > 2); } and calls that function, passing the current value that is being processed as a parameter to that function. Since you named the first parameter that your function accepts "i", you're now able to work with the variable i inside that function. If it returns "true", the value will be in the new array that filter() returns, if it returns "false", it will be skipped. That way, you can easily "filter" an array. Commented Jun 27, 2012 at 23:09
  • Also see Hamish's answer below, there's a pretty good explanation! Commented Jun 27, 2012 at 23:11
  • Thanks everyone, I've been coding c++ in school mostly so the idea of not having to manually do everything is a bit of a shock to me Commented Jun 27, 2012 at 23:19

6 Answers 6

25

.filter (Array.prototype.filter) calls the supplied function with 3 arguments:

function(element, index, array) {
    ...
  • element is the particular array element for the call.
  • index is the current index of the element
  • array is the array being filtered.

You can use any or all of the arguments.

In your case, i refers to the element and is used in the body of your function:

function(i){
    return (i > 2);
}

In other words, "filter elements where element is greater than 2".

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

4 Comments

Thanks Hamish, So i can call filter on an array of arrays? is that what the index and element is referring to? and I'm not sure I understand passing the array part. Isn't that already covered when I say numbers.filter. Does javascript pass a this pointer if I don't include anything?
If the array is an array of arrays, element will be an array. Put it this way, for each call to the function, element == array[index]. Not sure what the last sentence in your comment means, sorry.
If I omit the array and index like I have in the example where I just use i instead of element, index, array, then does javascript automatically pass the index and array? I'm not sure why I need or would want to include index and array?
index and array are passed, if you need them. If you don't need them for your particular filter, you don't need to worry about them. There are other filters that might depend on the index, or other elements in array, so they are provided for those cases.
3

i is a reference to the current object in the set when inside that closure. It could be named anything as it is just a variable, but then would have to have the same name inside the closure. Instead of using function(){} you could use a callback which is how filter was designed.

The reference is done implicitly by the definition of .filter, you can read more here: http://msdn.microsoft.com/en-us/library/ff679973(v=vs.94).aspx

Comments

0

The i is actually very important. It tells gives the filter function information about the elements it's acting on. In fact it's used right here (i > 2).

This keeps elements whose value is greater than 2.

Comments

0

That i is the formal parameter for the function you are supplying to .filter(). If you do not insert it, the function will not have any way¹ to refer to the argument it's being passed (the i inside the function body will then refer to some other entity that might not even be defined -- window.i would be typical).


¹ that is technically a lie, but consider it true for the purposes of this discussion

Comments

0

An old thread indeed, but just filling in what remains unsaid.

The parentheses are there for you the programmer to insert whatever variable name makes sense for your specific program.

If you choose 'i', most other (beginner) programmers might think 'Oh, i means index'. Which would be wrong.

If you use one argument instead of three, I'd choose 'el' to represent the element, or if your array contains flavors of soda, I'd choose 'flavor'.

Comments

0

That's ES5 notation and maybe if you see it in ES6 notation you would understand why the "i" is a must:

numbers.filter(i => i > 2);

A variable must always be used to refer to the item of the array that you process in each iteration (in this case "i"). It has to be passed as argument to the entry point of the function (in ES6 that goes before the arrow).

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.