语法:
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
参数:
callback
用来测试数组的每个元素的函数,返回true
表示该元素通过测试,保留该元素,false
则不保留,它接收3个参数
element
数组中当前正在处理的元素
index
可选
正在处理的元素在数组中的索引
array
可选
调用了 filter
的数组本身
thisArg
可选
执行 callback
时,用于 this
的值
返回值:
一个新的、由通过测试的元素组成的数组,如果没有任何元素通过测试,则返回空数组
实现:
Array.prototype.filter = function(callback, thisArg) {
if (this == undefined) {
throw new TypeError('this is null or undefined')
}
if (typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function')
}
const res = []
// 让 O 成为回调函数的对象传递(强制装换对象)
const O = Object(this)
// >>> 保证 len 为 number,且为正整数
const len = O.length >>> 0
for (let i = 0; i < len; i++) {
// 检查 i 是否在 O 的属性(会检查原型链)
if (i in O) {
// 回调函数调用传参
if (callback.call(thisArg, O[i], i, O)) {
res.push(O[i])
}
}
}
return res
}
对于 >>> 0
有疑问的,可以看看 解释 >>>0的作用