LoginSignup
0
0

More than 3 years have passed since last update.

TypeScript 配列でundefinedとnullを消す方法

Posted at

TS使っていて配列をfilterかけて取り出すとundefinedを考慮してくれた。

しかし、undefinedの消し方がわからず苦戦しました。

結論

  function notEmpty<TValue>(value: TValue | null | undefined): value is TValue {
    return value !== null && value !== undefined
  }
  const result = Data.filter(notEmpty)

NGな方法

const result = Data.filter(item => item !== undefined)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0