Skip to content

Instantly share code, notes, and snippets.

@trooperandz
Last active July 7, 2018 04:06
Show Gist options
  • Save trooperandz/128258ffed6d81657b5b10f6e095fb3b to your computer and use it in GitHub Desktop.
Save trooperandz/128258ffed6d81657b5b10f6e095fb3b to your computer and use it in GitHub Desktop.
Custom find prototype
// Create our custom find function and attach it to the prototype
Array.prototype.customFind = function(callback) {
const arr = this;
for (let i = 0; i < arr.length; i++) {
if (callback(arr[i], i, arr)) return arr[i];
}
return false;
}
// Execute our custom find function
const beachArr = ['waves', 'sharks', 'jellies', 'sand', 'pelicans'];
const customFindResult = beachArr.customFind(item => item.length === 4);
// sand
console.log(`Our custom find result: ${customFindResult}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment