سؤال

Ultimately what I hope to accomplish is creating a line graph by the total number of items created on a specific day. What I've done so far is use mapreduce to take care of the group by date portion. However, what I can't seem to do is take the result and process into something usable.

To give you an idea of what I mean, my mapreduce returns the following (the DateTime needs some work I'll get to that later):

[
    {
        "_id" => 1970-01-01 00:00:00 UTC, 
        "value" =>{ "count" => 25.0 }
    },              
    {
        "_id" =>2014-10-04 00:00:00 UTC,
        "value" =>{ "count"=>2.0 }
    }, 
    {
        "_id" => 2014-11-04 00:00:00 UTC,
        "value" => { "count"=> 2.0 }
    }, 
    {
        "_id" => 2014-12-04 00:00:00 UTC, 
        "value" => { "count"=> 9.0 }
    }
]

What I need to do is take each _id and show the count in such a way that I can easily pass that to google charts. So what I need it to look like is:

1970-01-01 00:00:00 UTC = 25  
2014-10-04 00:00:00 UTC = 2   
2014-11-04 00:00:00 UTC = 2  
2014-12-04 00:00:00 UTC = 9

But no matter what I try I can't see to loop through the array of nested hashes and pull the data I need into new variables. Any help would be greatly appreciated!

هل كانت مفيدة؟

المحلول

str = [{"_id"=>"1970-01-01 00:00:00 UTC", "value"=>{"count"=>25.0}}, {"_id"=>"2014-10-04 00:00:00 UTC", "value"=>{"count"=>2.0}}, {"_id"=>"2014-11-04 00:00:00 UTC", "value"=>{"count"=>2.0}}, {"_id"=>"2014-12-04 00:00:00 UTC", "value"=>{"count"=>9.0}}].map do |row|
  "#{row["_id"]} = #{row["value"]["count"].to_i}"
end.join("\n")

puts str
#>>
1970-01-01 00:00:00 UTC = 25
2014-10-04 00:00:00 UTC = 2
2014-11-04 00:00:00 UTC = 2
2014-12-04 00:00:00 UTC = 9
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top