Array.prototype.myFilter = function(fn = function(){}){
if(!(fn instanceof Function)) throw new Error("callback must be function");
const tempArr = []
for(let i = 0, len = this.length; i < len; i++){
const flag = fn(this[i], i, this);
flag && tempArr.push(this[i]);
}
return tempArr;
}
const myArr = [
{
id: 1,
name: 'wty'
},
{
id: 2,
name: '蓝之静云'
},
{
id: 3,
name: 'lys'
}
]
const res = myArr.myFilter(i => i.name === "wty");
console.log(res);
js自定义过滤器
最新推荐文章于 2022-05-18 09:54:50 发布