0

Say i have the two following arrays

array_1 = ["Person One", "Person Two", "Person Three"]
array_2 = ["24", "25", "26"]

How would I merge these arrays together so that the output would be

["Person One 24", "Person Two 25", "Person Three 26"]

Thanks

3 Answers 3

3

Use zip and join:

array_1.zip(array_2).map { |a| a.join(' ') }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer, this does work for a regular array, i misunderstood the construct of what i had (Capybara::Element tag="td"). but ill post a different question for that, thanks
1

You can do it following way:

array_1.each_with_index.map{ |el, i| el + " " + array_2[i].to_s }
# => ["Person One 24", "Person Two 25", "person Three 26"] 

Comments

1
array_1.zip(array_2).map { |a1, a2| "#{a1} #{a2}" }
#⇒ ["Person One 24", "Person Two 25", "Person Three 26"]

Comments

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.