我有这些数组:
array0 = [["1"], ["2"], ["3"],…]
array1 = [["a"], ["b"], ["c"],…]
array2 = [["pap"], ["pop"], ["pip"],…]
我想将每个数组中的每个元素推送到第一个数组,所以输出应该是:
output = [["1", "a", "pap"], ["2", "b", "pop"], ["3", "c" ,"pip"]]
我有这些数组:
array0 = [["1"], ["2"], ["3"],…]
array1 = [["a"], ["b"], ["c"],…]
array2 = [["pap"], ["pop"], ["pip"],…]
我想将每个数组中的每个元素推送到第一个数组,所以输出应该是:
output = [["1", "a", "pap"], ["2", "b", "pop"], ["3", "c" ,"pip"]]
首先flatten
对每个数组执行以获得一维数组。然后用于zip
获取每个具有 3 个元素的二维数组
array0.flatten.zip(array1.flatten, array2.flatten)
做同样事情的更短的方法:(@Ivaylo Strandjev 给出的解决方案)
array0.zip(array1, array2).map(&:flatten)
试试这个:
array0.zip(array1).zip(array2).map(&:flatten)
你也可以用一个zip
:
array0.zip(array1, array2).map(&:flatten)