I am using find () function
for one of my project. Official document https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find says that Internet Explorer is not supported. What else can I use?
Advertisement
Answer
A polyfill is a code that provides the functionality that you normally expect the browser to provide you natively. Here is the polyfill for Array.find
JavaScript
x
22
22
1
if (!Array.prototype.find) {
2
Array.prototype.find = function(predicate) {
3
if (this === null) {
4
throw new TypeError('Array.prototype.find called on null or undefined');
5
}
6
if (typeof predicate !== 'function') {
7
throw new TypeError('predicate must be a function');
8
}
9
var list = Object(this);
10
var length = list.length >>> 0;
11
var thisArg = arguments[1];
12
var value;
13
14
for (var i = 0; i < length; i++) {
15
value = list[i];
16
if (predicate.call(thisArg, value, i, list)) {
17
return value;
18
}
19
}
20
return undefined;
21
};
22
}