Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active June 8, 2021 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/5167299 to your computer and use it in GitHub Desktop.
Save WebReflection/5167299 to your computer and use it in GitHub Desktop.
Array.prototype.find and Array.prototype.findIndex quick and dirty polyfill
// WebReflection quick and dirty polyfill
(function(AP){
if ('find' in AP) return;
// quick size/safe version
// if predicate is not a function, call will throw
// if this is not array like, throws or nothing happens
function find(value) {
return function (predicate, thisArg) {
for(var
k = 0,
len = this.length;
k < len && !(
k in this &&
predicate.call(thisArg, this[k], k, this)
);
k++
);
return value ? this[k] : k === len ? -1 : k;
};
}
//https://gist.github.com/rwldrn/5079436
AP.find = find(!0);
// https://gist.github.com/rwldrn/5079427
AP.findIndex = find(!1);
}(Array.prototype));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment