I’m having trouble understanding how to solve this issue, essentially I have two arrays that look like:
First array:
days_to_report
#=> [{:date=>Fri, 01 Apr 2011},
#=> {:date=>Sat, 02 Apr 2011},
#=> {:date=>Sun, 03 Apr 2011},
#=> {:date=>Mon, 04 Apr 2011}]
Second array:
tracks_by_day
#=> [{:date=>Mon, 04 Apr 2011, :count=>905, :percentage=>13.205895228367137},
#=> {:date=>Sat, 02 Apr 2011, :count=>6745, :percentage=>98.4240478622501},
#=> {:date=>Fri, 01 Apr 2011, :count=>6853, :percentage=>100.0}]
The dates being Date objects in this case.
So I want to like merge them so that for every item in the first array (days_to_report), has the data that the second array has too, like the ideal output would be: (notice the data for the date for Sun, 03 Apt 2011)
[{:date=>Mon, 04 Apr 2011, :count=>905, :percentage=>13.205895228367137},
{:date=>Sun, 03 Apr 2011, :count=>0, :percentage=>0},
{:date=>Sat, 02 Apr 2011, :count=>6745, :percentage=>98.4240478622501},
{:date=>Fri, 01 Apr 2011, :count=>6853, :percentage=>100.0}]
Notice that the second item of the array has zero for the values because the :date wasn’t in the hash for the tracks_by_day array… any thoughts on how to accomplish this? It seems like the functionality provided in zip or something, but I need to match up the keys to see if they exist and zero fill their values if they don’t.
I hope I’m describing this correctly, thanks again guys! high five!
OR