试试这个。请注意,最终产品是散列,而不是散列数组。但我认为在这种情况下哈希更容易使用。
# starting variables
array_of_months = ["05", "06", "07", "08", "09", "10", "11", "12", "01", "02", "03", "04", "05"]
month_sums = [{"month"=>5, "month_sum"=>20}, {"month"=>4, "month_sum"=>100}]
# clean up array_of_months
months = array_of_months.compact.sort
=> ["01", "02", "03", "04", "05", "05", "06", "07", "08", "09", "10", "11", "12"]
# compress month_sums into single key/value pairs such that first value becomes the key and second value becomes the value
sums = month_sums.inject({}) { |a, ms| a.merge!("%02d" % ms['month'] => ms['month_sum']) }
=> { "05" => 20, "04" => 100 }
# generate hash of all months and match sums value if key is present otherwise assign value zero
all_month_sums = months.inject({}) { |h, m| h.merge!(m => sums[m] || 0) }
=> {"01"=>0, "02"=>0, "03"=>0, "04"=>100, "05"=>20, "06"=>0, "07"=>0, "08"=>0, "09"=>0, "10"=>0, "11"=>0, "12"=>0}
编辑(根据新信息)
# starting variables
months = ["05", "06", "07", "08", "09", "10", "11", "12", "01", "02", "03", "04", "05"]
month_sums = [{"month"=>5, "month_sum"=>20}, {"month"=>4, "month_sum"=>100}, {"month" => 5, "month_sum" => 99 }]
# iterate each month, select the first match, remove the match when done. if no match just give month a zero.
months.inject([]) do |a, month|
if s = month_sums.select { |s| month.to_i == s['month'] }.first
a << { "%02d" % s['month'] => s['month_sum'] }
s['month'] = nil
else
a << { month => 0 }
end
a
end
=> [{"05"=>20}, {"06"=>0}, {"07"=>0}, {"08"=>0}, {"09"=>0}, {"10"=>0}, {"11"=>0}, {"12"=>0}, {"01"=>0}, {"02"=>0}, {"03"=>0}, {"04"=>100}, {"05"=>99}]