开发者

How to flatten an array of arrays - but not all the way down

开发者 https://www.devze.com 2023-02-23 03:41 出处:网络
What\'s the best way to convert this [[[\"Club three Team one\", 7800], [\"Club three Team two\", 7801]], [], [[\"Club four Team one\", 7807], [\"Club four Team two\", 7808]], []]

What's the best way to convert this

[[["Club three Team one", 7800], ["Club three Team two", 7801]], [], [["Club four Team one", 7807], ["Club four Team two", 7808]], []]

into

[["Club three Team one", 7800], ["Club three Team two", 7801], ["Club four Team one", 7807], ["Club four Team two", 7808]]

in ruby? flatten converts this all the way down to

["Club three Team one", 7303, "Club three Team two", 7304, "Club four Team one", 7310, "Clu开发者_如何学Pythonb four Team two", 7311]


use 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]]
0

精彩评论

暂无评论...
验证码 换一张
取 消