Skip to content

2634. Filter Elements from Array 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
type Fn = (n: number, i: number) => any;

function filter(arr: number[], fn: Fn): number[] {
  const ans: number[] = [];
  arr.forEach((a, index) => {
    if (fn(a, index)) {
      ans.push(a);
    }
  });
  return ans;
}