js filter方法 数组去重 哪位boss能讲解下原理或者思路
Array.prototype.myFilter = function(callback, thisArg) {
// 获取数组的长度,确定循环的次数
const length = this.length
let newArray = []
for (let index = 0; index < length; index++) {
// 利用hasOwnProperty方法过滤原数组空值和已经删除的值
if (this.hasOwnProperty(index)) {
// 利用call方法改变callback函数this的指向
if (callback.call(thisArg, this[index], index, this)) newArray.push(this[index])
}
}
return newArray
}