1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| Array.prototype.myFilter = function (callback, thisArg) { if (this == null) { throw new TypeError("this is null or not defined"); }
if (typeof callback !== "function") { throw new TypeError(callback + " is not a function"); }
const res = [];
const O = Object(this);
const len = O.length >>> 0;
for (let i = 0; i < len; i++) {
if (i in O) {
if (callback.call(thisArg, O[i], i, O)) { res.push(O[i]); } } }
return res; };
let arr = [1, 2, 3, 4, 5];
let newArr = arr.myFilter(function (item) { return item > 3; });
console.log(newArr);
|