Y
Hacker News
new
|
ask
|
show
|
jobs
by
knrz
2374 days ago
In JavaScript, Elixir, and Ruby I use flatMap for this purpose. You can return an empty array on the block to “skip” over the element.
1 comments
Rafert
2374 days ago
2.7 introduces `Enumerable#filter_map` for that. Example from the NEWS file:
[1, 2, 3].filter_map {|x| x.odd? ? x.to_s : nil } #=> ["1", "3"]
link
[1, 2, 3].filter_map {|x| x.odd? ? x.to_s : nil } #=> ["1", "3"]