Reputation: 277
I have 2 arrays in ruby
firstarray = ["1212","3434"]
secondarray = ["9999","6666","7777"]
I want to merge these 2 arrays into thirdarray and thirdarray should have following structure-
thirdarray = ["1212","3434","9999","6666","7777"]
I was planning to use this:
thirdarray = [firstarray, secondarray.join(", ")]
but this gives me below which does not have " " around individual values 9999, 6666, 7777.
["1212", "3434", "9999 , 6666 , 7777"]
How can I do that?
Upvotes: 0
Views: 77
Reputation: 10251
another way:
> thirdarray = [*firstarray, *secondarray]
#=> ["1212", "3434", "9999", "6666", "7777"]
if you want to add extra elements:
> thirdarray = [*firstarray, *secondarray, "additional-1" ]
#=> ["1212", "3434", "9999", "6666", "7777", "additional-1"]
Upvotes: 0
Reputation: 649
You can also use concat operator.
Below edits your 'firstarray' by appending your 'secondarray' elements to the end of 'firstarray'. concat is more performant than '+'
firstarray.concat(secondarray)
Upvotes: 0
Reputation: 124
Just use the +
operator on those two arrays.
firstarray = ["1212","3434"]
secondarray = ["9999","6666","7777"]
thirdarray = firstarray + secondarray
=> ["1212", "3434", "9999", "6666", "7777"]
Upvotes: 4