使用ruby在另一个复杂数组中包含一个数组

时间:2012-03-10 10:57:54

标签: ruby arrays

我有以下数组:

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

我怎样才能将它们组合起来,所以我得到了这个数组:

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

2 个答案:

答案 0 :(得分:3)

与robinst相同,但更短

a1 = [ ["link_text1", "link1"],["link_text2", "link2"] ]
a2 = [ ["views1"],["views2"] ]
a1.zip(a2).map(&:flatten)

答案 1 :(得分:0)

尝试拉链和展平的组合:

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"]]