Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove "empty" elements from multidimensional array

I have a multidimensional array something like this

[ [[]], [[1], [2]], [[1, 2]] ]

What's the best way to remove the empty array?

Right now I am just doing a array[1..-1] to remove the first element but I would like a more reliable way to do it.

like image 860
Brand Avatar asked Feb 23 '23 14:02

Brand


1 Answers

Flatten each array and if it has no elements in it, delete it.

arr = [ [[]], [[1], [2]], [[1, 2]] ]
arr = arr.delete_if { |elem| elem.flatten.empty? }
# => [[[1], [2]], [[1, 2]]]
like image 160
Douglas F Shearer Avatar answered Mar 08 '23 10:03

Douglas F Shearer