如何展平阵列数组 - 但不是一直向下

时间:2011-04-10 15:04:47

标签: ruby-on-rails ruby

转换此

的最佳方法是什么?
[[["Club three Team one", 7800], ["Club three Team two", 7801]], [], [["Club four Team one", 7807], ["Club four Team two", 7808]], []]

进入

[["Club three Team one", 7800], ["Club three Team two", 7801], ["Club four Team one", 7807], ["Club four Team two", 7808]]
红宝石? flatten将其一直转换为

["Club three Team one", 7303, "Club three Team two", 7304, "Club four Team one", 7310, "Club four Team two", 7311]

1 个答案:

答案 0 :(得分:104)

使用flatten(1) http://apidock.com/ruby/Array/flatten

your_array = [[["Club three Team one", 7800], ["Club three Team two", 7801]], [], [["Club four Team one", 7807], ["Club four Team two", 7808]], []]
your_array.flatten(1)
#=> [["Club three Team one", 7800], ["Club three Team two", 7801], ["Club four Team one", 7807], ["Club four Team two", 7808]]