7

How can you change this array:

[["1","one"], ["2","two"], ["3","three"]]

to this?

["1","one"], ["2","two"], ["3","three"]

Clarification

My apologies for giving an invalid second version. This is what I'm really going for:

I want to add ["0","zero"] to the beginning of [["1","one"], ["2","two"], ["3","three"]], to get:

[["0","zero"], ["1","one"], ["2","two"], ["3","three"]]

I have tried:

["0","zero"] << [["1","one"], ["2","two"], ["3","three"]]

The above approach produces this, which contains a nesting I don't want:

[["0","zero"], [["1","one"], ["2","two"], ["3","three"]]]
5
  • 2
    Your second one isn't even valid... Commented May 19, 2011 at 23:32
  • Is it possible you meant [{"1"=>"one"}, {"2"=>"two"}, {"3"=>"three"}] Commented May 19, 2011 at 23:39
  • @mathepic, @ItayMoav - I just clarified my question. My apologies for not being clearer. Commented May 19, 2011 at 23:46
  • I think you should show some source code. I think it would help if we looked at how you are building the array in the first place. I suspect a misuse of << when you probably should use += instead. Commented May 20, 2011 at 0:03
  • I am trying to reach the same thing and I am stuck here... I am trying exactly now to make [[1,"A"],[2,"B"],[3,"C"],[4,"D"],[5,"E"]] into [1,"A"],[2,"B"],[3,"C"],[4,"D"],[5,"E], I remind that I already did one day it but I don't remember how could I do that, I was to open another topic here at SO but I saw you now and I am trying to take opportunity of the doubt, I have tried flatten but it returns me [1,"A",2,"B",3,"C",4,"D",5,"E] and not [1,"A"],[2,"B"],[3,"C"],[4,"D"],[5,"E] Commented Aug 9, 2021 at 18:27

4 Answers 4

7

unshift ought to do it for you:

a = [["1","one"], ["2","two"], ["3","three"]]
a.unshift(["0", "zero"])
=> [["0", "zero"], ["1", "one"], ["2", "two"], ["3", "three"]]
Sign up to request clarification or add additional context in comments.

4 Comments

@WayneConrad - almost there. When I do this: [["Inbox", 0]] << current_user.folders.collect{|folder| [folder.name, folder.id]}.flatten(1), I get: [["Inbox", 0], ["My Folder", 132, "Fourth Folder", 133, "Third Folder", 134, "Fifth Folder", 135, "6th Folder", 136]]
@Andrew Grimm, the flatten(1) is gone (the OP changed the question), so you can take away the +1 if you like :)
@WayneConrad - I still appreciate the flatten(1). Thank you very much for that.
@WayneConrad - that's it! Thank you very much and sorry for that sloppy first question. +1
5

You are probably looking for flatten:

Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array. If the optional level argument determines the level of recursion to flatten.

[["1","one"], ["2","two"], ["3","three"]].flatten

Which gives you:

=> ["1", "one", "2", "two", "3", "three"] 

1 Comment

And to unnest the first array level?? I have tried flatten as how you showed but it returns me [1,"one",2,"two",3,"three"] and not [1,"one"],[2,"two"],[3,"three"]!
3

[["1","one"], ["2","two"], ["3","three"]].flatten

Comments

0

You need to make your questions clearer, but is the following what you meant?

# Right answer
original = [["1","one"], ["2","two"]]
items_to_add = [["3","three"], ["4","four"]]
items_to_add.each do |item|
  original << item
end
original # => [["1", "one"], ["2", "two"], ["3", "three"], ["4", "four"]]

# Wrong answer
original = [["1","one"], ["2","two"]]
items_to_add = [["3","three"], ["4","four"]]
original << items_to_add # => [["1", "one"], ["2", "two"], [["3", "three"], ["4", "four"]]]

1 Comment

Thanks a lot Andrew. WayneConrad got the answer I was looking for using unshift. For posterity I've further clarified my original question.

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.