Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts

    the filter method of array

    Hey all,

    Quick question. I was reading this book art/science of javascript and it defines its own custom filter method:

    Code:
    if (!Array.prototype.filter) { 
    	Array.prototype.filter = function(func, scope) { 
    	scope = scope || this; 
    	var list = []; 
    	for (var i = 0, l = this.length; i < l; i++) 
    	if (func.call(scope, this[i], i, this)) list.push(this[i]); 
    	return list; 
    	} 
    }
    I honestly don't understand this line right here "if (func.call(scope, this[i], i, this)) list.push(this[i]); " and what contribtues to that is that the book doesn't provide an example of the filter method being used. So I was wondering if anyone can provide a practical example of how to use this method.

    Thanks for any response.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    I found another similar example, but notice how the call is sending for arguments: fun.call(thisp, this[i], i, this);
    Yet the function (which is invoked by call()) is only receiving three arguments:
    printBr(element, index, array)

    How is this possible?

    Also, why would we be passing the second index of the array right here: var thisp = arguments[1];
    I don't see the point of that.

    Code:
    <html>
    <head>
    <title>JavaScript Array forEach Method</title>
    </head>
    <body>
    <script type="text/javascript">
    if (!Array.prototype.forEach)
    {
      Array.prototype.forEach = function(fun /*, thisp*/)
      {
        var len = this.length;
        if (typeof fun != "function")
          throw new TypeError();
    
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
          if (i in this)
            fun.call(thisp, this[i], i, this);
        }
      };
    }
    
    function printBr(element, index, array) {
      document.write("<br />[" + index + "] is " + element ); 
    }
    
    [12, 5, 8, 130, 44].forEach(printBr);
      
    </script>
    </body>
    </html>
    Last edited by johnmerlino; 06-14-2010 at 06:42 PM.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    do you know how the Function.call() function works?

    parameters 2 to 4 are passed to the function called, parameter 1 is used by .call() itself.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by johnmerlino View Post

    Also, why would we be passing the second index of the array right here: var thisp = arguments[1];
    I don't see the point of that.
    you aren't. you are specifing what this means in the 2nd argument to filter.


    for example, we can use "this" as an extra argument to help create generic functions:


    Code:
    function gt(a){ return a > this ; }
    
    var r=[1,2,3,4,5,6];
    
    
    alert( r.filter(  gt,  3  ));
    in this example, the array r is just some numbers.
    the comparator function gt returns true if it's this is less than it's argument.

    by mapping gt to the array in a filter method, we remove all elements whose value is less than this.

    when i set this to 3, (by passing a 2nd arg to filter), the alert shows "4,5,6"; the numbers that are greater than 3.


    does that make sense?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    johnmerlino (06-17-2010)

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    So basically:

    i = to the index number
    this[i] = equal to the integer or DOM element
    this = equal to the array
    pthis = equal to a specific integer or DOM element in array depending on what we passed from the arguments method - arguments[1] will pass the second index value.

    Is this correct?
    Thanks.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    thisp is the object that this inside fun will point to.
    this[i] is the i(th) Array element/item

    if you don’t declare the thisp parameter in the function call (it is commented out in the example), you have to get it another way, that is by using the (internal) variable arguments holding the parameters passed to the function.
    Last edited by Dormilich; 06-17-2010 at 07:28 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    johnmerlino (06-18-2010)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •