语法: var newArray = arr.filter(callbackFn(currentValue[, index[, array]])[, thisArg])
方法功能: 创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
返回:一个新的、由通过测试的元素组成的数组,如果没有任何数组元素通过测试,则返回空数组。
自定义函数:myFilter。
Array.prototype.myFilter = function(callbackFn, thisArg) {
if (typeof callbackFn !== 'function') throw ('callbackFn参数必须是函数');
let element = this,
len = element && element.length || 0,
result = [];
if (!thisArg) thisArg = element;
for (let index = 0; index < len; index++) {
if (callbackFn.call(thisArg, element[index], index, element)) result.push(element[index]);
}
return result;
};