-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
40 lines (34 loc) · 955 Bytes
/
filter.js
File metadata and controls
40 lines (34 loc) · 955 Bytes
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
/**
* @author songchengen
* @date 2018/7/15
* @description Array.prototype.filter
*/
/**
* 简单实现数组原过滤功能
* @param {Function} callback 回调函数
* @param {any} arr 遍历对象
* @param {any} thisArg 回调函数的this指针
* @returns {any[]} 过滤后的数组
*/
const filter = (callback, arr, thisArg) => {
// 参数校验
if (arr == null) throw new TypeError('arr is null or not defined');
if (Object.prototype.toString.call(callback) !== '[object Function]') throw new TypeError(`${callback} is not a function`);
// 原:ToObject(arr)
const O = Object(arr);
// get(O, 'length') 原:O.length >>> 0
const len = O.length;
// 原:Let A be ? ArraySpeciesCreate(O, 0).
const R = [];
const k = 0;
while (k < len) {
const Pk = String(k);
if (Pk in O) {
if (callback.call(thisArg, O[Pk], k, O)) {
R.push(O[Pk]);
}
}
}
return R;
};
module.exports = filter;