---
# System prepended metadata

title: 2634. Filter Elements from Array
tags: [easy, JavaScript]

---

# [2634. Filter Elements from Array](https://leetcode.cn/problems/filter-elements-from-array/description/?envType=study-plan-v2&envId=30-days-of-javascript)

![image](https://hackmd.io/_uploads/Hk6agfcM0.png)

題目希望我們使用手寫原生陣列 filter 方法
所以我們先設定一個新的array
將fn(arr[i],i)放入fn確認
如果符合if放入新的array
```JS=
var filter = function(arr, fn) {
    const filteredArr  = [];
    for(let i = 0;i < arr.length;i++){
        if (fn(arr[i], i)) {
            filteredArr.push(arr[i]);
        }
    }
    return filteredArr;
};
```