1

How would I combine the subarrays within this array

[["0", "0", "0"], ["0", "0", "0"], ["1"]]

I want to add a comma in between each subarrays so that it would read 000,000,1.

I have tried using *"," but it will combine the whole array together and give me 0,0,0,0,0,0,1 instead. Thanks for your help!

1
  • 000,000,1 is not an object. Is it "000,000,1", or ["000","000","1"] or something else? Commented Feb 26, 2015 at 6:48

1 Answer 1

4

First join the inner arrays:

 array.map(&:join)
 # => ["000", "000", "1"]

That join that array, but this time with a , as a separator:

 array = ["000", "000", "1"]
 array.join(',')
 # => "000,000,1"

Or as a one liner:

 array = [["0", "0", "0"], ["0", "0", "0"], ["1"]]
 array.map(&:join).join(',')
 # => "000,000,1"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.