0

I have the following arrays:

array = [ [link_text1, link1],[link_text2, link2], ... ]
array = [ [views1],[views2], ... ]

How can I can combine them, so I get this array:

[ [link_text1, link1, views1], [link_text2, link2, views2], ... ]
1
  • Are you trying to get a cartesian product? Commented Mar 10, 2012 at 11:11

2 Answers 2

3

The same as robinst, but a little shorter

a1 = [ ["link_text1", "link1"],["link_text2", "link2"] ]
a2 = [ ["views1"],["views2"] ]
a1.zip(a2).map(&:flatten)
Sign up to request clarification or add additional context in comments.

Comments

0

Try a combination of zip and flatten:

a1 = [ ["link_text1", "link1"],["link_text2", "link2"] ]
a2 = [ ["views1"],["views2"] ]
zipped = a1.zip(a2)
array_final = zipped.collect { |a| a.flatten }
#=> [["link_text1", "link1", "views1"], ["link_text2", "link2", "views2"]]

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.