Skip to content

Instantly share code, notes, and snippets.

@suzusuzu
Created July 9, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzusuzu/10d352458bd0ccf48c559d75325de41b to your computer and use it in GitHub Desktop.
Save suzusuzu/10d352458bd0ccf48c559d75325de41b to your computer and use it in GitHub Desktop.
non null filter implementation in typescript
const arr = [...Array(100).keys()];
const arr1 = arr.map(x => x*x % 2 == 0 ? x*x : null).filter((x): x is number => x !== null);
// arr1: number[]
const arr2 = arr.flatMap(x => x*x % 2 == 0 ? [x*x] : [])
// arr2: number[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment