Skip to content

Commit 8cc2ee3

Browse files
committed
feat: 完成手写filter函数
1 parent dbfdf09 commit 8cc2ee3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

面试常见的手写题/14-filter.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const arr = [1, 2, 3, 4, 5]
2+
3+
Array.prototype._filter = function (fn) {
4+
const newArr = []
5+
for (let i = 0; i < this.length; i++) {
6+
if (fn(this[i])) newArr.push(this[i])
7+
}
8+
return newArr
9+
}
10+
11+
const newArr = arr._filter((item) => item > 2)
12+
13+
console.log(newArr)

0 commit comments

Comments
 (0)