# 2634. Filter Elements from Array
###### tags: `leetcode 30 days js challenge` `Easy`
[2634. Filter Elements from Array](https://leetcode.com/problems/filter-elements-from-array/)
### 題目描述
Given an integer array `arr` and a filtering function `fn`, return a new array with a fewer or equal number of elements.
The returned array should only contain elements where `fn(arr[i], i)` evaluated to a truthy value.
Please solve it without the built-in `Array.filter` method.
### 範例
**Example 1:**
```
Input: arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
Output: [20,30]
Explanation:
const newArray = filter(arr, fn); // [20, 30]
The function filters out values that are not greater than 10
```
**Example 2:**
```
Input: arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
Output: [1]
Explanation:
fn can also accept the index of each element
In this case, the function removes elements not at index 0
```
**Example 3:**
```
Input: arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
Output: [-2,0,1,2]
Explanation:
Falsey values such as 0 should be filtered out
```
**Constraints**:
- `0 <= arr.length <= 1000`
- -10^9^ <= `arr[i]` <= 10^9^
### 解答
#### TypeScript
```typescript=
function filter(arr: number[], fn: (n: number, i: number) => any): number[] {
const newArr: number[] = [];
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i], i)) {
newArr.push(arr[i]);
}
}
return newArr;
}
```
> [name=Sheep][time=Tues, May 9, 2023]
### Reference
[回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)