Created
May 31, 2018 20:43
-
-
Save trooperandz/b9a5f0fc7e36d29990792fab858439c0 to your computer and use it in GitHub Desktop.
Custom filter prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create our custom filter function and attach it to the prototype | |
Array.prototype.customFilter = function(callback) { | |
const arr = this; | |
const newArr = []; | |
for(let i = 0; i < arr.length; i++) { | |
if (callback(arr[i], i, arr)) newArr.push(arr[i]); | |
} | |
return newArr; | |
} | |
// Execute our custom filter function | |
const beachArr = ['waves', 'sharks', 'jellies', 'sand', 'pelicans']; | |
const newArr = beachArr.customFilter(item => item.length > 5); | |
// ['sharks', 'jellies', 'pelicans'] | |
console.log(`Our newly filtered array: ${newArr}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment