Search code examples
javascriptarraysfilterhasownproperty

Array filter returns strange results


Related to this question, i wanted to try out this

var arr = [0,1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]];
arr.filter(Object.hasOwnProperty,"abc");//outputs [0, 1, 2]
arr.filter(Object.hasOwnProperty,"2222222") //[0, 1, 2, 4, 6]

Does anyone knows why filter return these values? Spec of filter and MDN doc also doesn't clearly tell how second argument of filter is used.


Solution

  • The second argument to the Array.prototype.filter is the value that will be set as this to the function that is passed as a first argument.

    So your code ends up to be something like:

    arr.filter(function(v, i, a) {
        return Object.hasOwnProperty.call("222", v, i, a);
    });
    

    So it basically checks if the "222" string has the properties you enumerate in the array.

    From that it becomes clear why properties 0, 1 and 2 are found - since those are the indexes of the characters in the "222" string, and, say, 9 or {"abc":123} are not - since "222" string does not have such a properties.

    It is the same story with the longer string, which also includes properties 4 and 6 just because it's longer.

    Some examples:

    Object.hasOwnProperty.call("222", 1); // true, because `"222"[1]` is there
    Object.hasOwnProperty.call("222", 'foo'); // false, because `"222"['foo']` is not there