Question

I have an array with some values:

[1, 2, 3, 4]

I'd like to make a new array that contains mapped version of the items in the array above, but only add them to the new array if they pass a truth test.

A combination of map and filter?

[1, 2, 3, 4].mapFilter(function(n) { if (n > 2) return n * 3 })

What is this called?

Was it helpful?

Solution

This is map composed with filter. It doesn't need a name.

map (*3) . filter (>2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top