Javascript Array flatMap() add and remove elements

Introduction

Javascript array flatMap() can add and remove items during mapping.

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



PreviousNext

Related