How do I turn this:
first_array = [
{:count=>nil, :date=>"Jan 31"},
{:count=>nil, :date=>"Feb 01"},
{:count=>nil, :date=>"Feb 02"},
{:count=>nil, :date=>"Feb 03"},
{:count=>nil, :date=>"Feb 04"},
{:count=>nil, :date=>"Feb 05"}
]
second_array = [
{:count=>12, :date=>"Feb 01"},
{:count=>2, :date=>"Feb 02"},
{:count=>2, :date=>"Feb 05"}
]
Into this:
result = [
{:count=>nil, :date=>"Jan 31"},
{:count=>12, :date=>"Feb 01"},
{:count=>2, :date=>"Feb 02"},
{:count=>nil, :date=>"Feb 03"},
{:count=>nil, :date=>"Feb 04"},
{:count=>2, :date=>"Feb 05"}
]
I have found similar questions on SO, but none were as simple as this one. There's probably a method/block-combination I should use I don't know of.
result_array = first_array.map do |first_hash|
second_array.each do |second_hash|
if first_hash[:date] == second_hash[:date]
first_hash[:count] = second_hash[:count]
break
end
end
first_hash
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With