You could do that, but I’d argue using filter then map is more readable. What do empty arrays have to do with doubling even integers?
ls.flatMap(x => {
if (x < 0) {
return []
} else if (x == 0) {
return [0]
} else {
return [Math.sqrt(x), -Math.sqrt(x)]
}
})
gives you all the real square roots from the original list, doing the mapping, flattening, and filtering all in one function call. ls.filter(o=>0<o).map(o=>o||[Math.sqrt(x, -Math.sqrt(x)])nothing, but they do have some relationship to 0, "" and Promise.resolve() - the array is handling the logic that will make the results be combined, not the doubling part