OneMoreError
OneMoreError

Reputation: 7728

Reduce a multi-dimensional array into a smaller multi-dimensional array

I wanted to reduce a multi-dimensional array into a smaller multi-dimensional array. Let me post what I mean. Here is my input/starting array:

[
  [
    [ ["Armando", "P"], ["Dave", "S"] ],
    [ ["Richard", "R"],  ["Michael", "S"] ],
  ],
  [
    [ ["Allen", "S"], ["Omer", "P"] ],
    [ ["David E.", "R"], ["Richard X.", "P"] ]
  ]
]

And I think this is the four dimensional array which I want to reduce to:

["Armando", "P"], ["Dave", "S"], ["Richard", "R"],  ["Michael", "S"], ["Allen", "S"],["Omer", "P"] , ["David E.", "R"], ["Richard X.", "P"] 

How can I do this in Ruby?

Upvotes: 2

Views: 513

Answers (1)

tokland
tokland

Reputation: 67900

Use Array#flatten(levels):

xs.flatten(2)

Upvotes: 3

Related Questions