Skip to content

Instantly share code, notes, and snippets.

@trooperandz
Created May 31, 2018 20:43
Show Gist options
  • Save trooperandz/b9a5f0fc7e36d29990792fab858439c0 to your computer and use it in GitHub Desktop.
Save trooperandz/b9a5f0fc7e36d29990792fab858439c0 to your computer and use it in GitHub Desktop.
Custom filter prototype
// 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