Javascript Problems – Create my own Array.prototype.filter() method – myFilter()

The purpose of this exercise is to create my own Array.prototype.filter() method – myFilter().  On line 2, I add a new myFilter() method, which takes in a function as an argument, to the array object.  I loop through each item in the array that called the method, and on line 6 determine if the function has returned anything for that item.  If so, I push it to my new output array on line 7.

On line 52, I use the myFilter() method on the “list” array of a few numbers.  I call the isTen function, which returns numbers greater than 10.  On line 53, I use the myFilter() method on the “arr” array (an array of objects) to call the filterID function which will only return ids that are a valid number.  Finally, on line 54, I use the myFilter() method to call the filterFruit function on the fruits array.  It returns any fruits with the letter “ap” in the word: apPLE and GRapE.

I took my sample problem data from the MDN Developer Array.prototype.filter() page.

Leave a Reply

Your email address will not be published.