Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created August 12, 2020 09:27
Show Gist options
  • Save gkucmierz/f8f7732e46a418c482ca010a74fef60b to your computer and use it in GitHub Desktop.
Save gkucmierz/f8f7732e46a418c482ca010a74fef60b to your computer and use it in GitHub Desktop.
// 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