Created
August 12, 2020 09:27
-
-
Save gkucmierz/f8f7732e46a418c482ca010a74fef60b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// find_value.js | |
const arr = ['a', 'b', 'c', 'd']; | |
const stopAt = 'c'; | |
console.log( | |
arr.findIndex((...args) => { | |
console.log('args', args); | |
if (args[0] === stopAt) return true; | |
return false; | |
}) | |
); | |
const findValue = (arr, fn) => { | |
const idx = arr.findIndex(fn); | |
if (idx === -1) return; | |
return arr[idx]; | |
}; | |
console.log('-----'); | |
Array.prototype.findValue = function(fn) { | |
return findValue(this, fn); | |
}; | |
console.log( | |
arr.findValue((...args) => { | |
console.log('args', args); | |
if (args[0] === stopAt) return true; | |
return false; | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment