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

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


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, "Club four Team two", 7311]

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAddyView Question on Stackoverflow
Solution 1 - Ruby on-Railsfl00rView Answer on Stackoverflow