Javascript array flatMap() can add and remove items during mapping.
flatMap()
The following code removes all the negative numbers.
let a = [-5, 4, -3] let b = a.flatMap( (n) => (n < 0) ? [] :[n]); console.log(b);// w ww . j a va 2 s . c o m