将数组数组与其他数组合并

时间:2013-06-07 08:54:59

标签: ruby arrays hash

我明白了:

first = [['lorem1','lorem1','lorem1',...],['lorem2','lorem2','lorem2',...],...]
second = [value1,value2,value3, ...]

我想做那样的事情:

my_array_of_hash = [{value 1 =>lorem1, value2 => lorem1}, {value1 =>lorem2, value2 => lorem2}, .... ]

你可以帮帮我吗?

1 个答案:

答案 0 :(得分:3)

hashes_array = []
first.each do |array|
  hashes_array << Hash[second.zip array]
end

或:

hashes_array = first.map do |array|
  Hash[second.zip array]
end