Skip to content

Instantly share code, notes, and snippets.

@oboshto
Created October 24, 2016 11:27
Show Gist options
  • Save oboshto/85afc4be8bc0067401c6e255b42101fe to your computer and use it in GitHub Desktop.
Save oboshto/85afc4be8bc0067401c6e255b42101fe to your computer and use it in GitHub Desktop.
Array.prototype.find (polyfill)
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment