0

Filtering out the undefined values in array, but somewhat the following code doesnt filter out undefined values with filter function.

How to make it work with filter function, and why do these code not work?

Thanks

TS version: 4.9.4

TS Playground


const dirtyNums = [1, 3, 4, 6, undefined, 4 , undefined]

// None of these filter out undefined values
const pureNums1: number[] = dirtyNums.filter(num => num)
const pureNums2: number[] = dirtyNums.filter(num => !!num)
const pureNums3: number[] = dirtyNums.filter(num => num !== undefined)
const pureNums4: number[] = dirtyNums.filter(num => num != null)

Pytan
  • 1,138
  • 11
  • 28

0 Answers0