4 ms·
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.
by knrz 7y 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.
- Rafert 7y ago2.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"]