0

I have the following array structure:

[[a], [b, c, [d, e]]]

That could have many levels of nesting (defined in execution time)

I would like to transform it in:

[a], [b,c], [d, e], ...

I have not found a way that works for 100% of the cases.

Any suggestion?

Thank you,

2
  • 4
    What do you mean by [a], [b,c], [d, e] - do you mean [ [a], [b,c], [d, e] ] Commented Jul 12, 2011 at 2:08
  • 3
    Array#flatten doesn't solve this problem as stated. Commented Jul 12, 2011 at 3:11

2 Answers 2

4

Array#flatten should do the trick?

Sign up to request clarification or add additional context in comments.

7 Comments

flatten will return [a,b,c,d,e] for the example input.
Thank you. Ir worked. Also tried: a * "," Where "a" is the nested structure. But your solutions is more elegant
@Gregor: Don't forget to accept answers that solve your problem.
@Andrew: Sure, but that wouldn't give you the sample output either. [[a], [b, c, [d, e]]].flaten(1) is [ a, b, c, [d, e] ], not [ [a], [b,c], [d, e] ].
@sepp2k is right..how did you get the output using Array#flatten? or is ur output different from that is stated in the question..your expected output in the question doesnt make much sense..
|
0

Yes, the output using flatten is

 [a, c, d, e] (depending of the level of recursion you use in flatten)

In fact, that output worked for me, I was over complicating desiring the [ [a], [b,c], [d, e] ]

Never the less, could be interesting to know the process for that particular output.

1 Comment

gosh, this was supposed to be a comment for the answer! Sorry!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.