Last active
July 7, 2018 04:06
-
-
Save trooperandz/128258ffed6d81657b5b10f6e095fb3b to your computer and use it in GitHub Desktop.
Custom find 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 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