filter through array of arrays ruby
array = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
result = array.flat_map { |sub_array| sub_array.select(&:even?) }
array
is initialized as an array of arrays containing integers.flat_map
iterates through each sub-array.- Within each sub-array,
select
filters elements based on the condition&:even?
, which checks if the element is even. - The selected elements are flattened and stored in the
result
variable as a single array.