2011-02-27 237 views
1

我有一個看起來像這樣的數組。拼合紅寶石陣列

[{"title"=>"ga:browser=Internet Explorer", "dimensions"=>[{:browser=>"Internet Explorer"}], "metrics"=>[{:pageviews=>2047}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Internet%20Explorer&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Safari", "dimensions"=>[{:browser=>"Safari"}], "metrics"=>[{:pageviews=>1196}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Safari&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Firefox", "dimensions"=>[{:browser=>"Firefox"}], "metrics"=>[{:pageviews=>835}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Firefox&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Chrome", "dimensions"=>[{:browser=>"Chrome"}], "metrics"=>[{:pageviews=>227}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Chrome&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Mozilla Compatible Agent", "dimensions"=>[{:browser=>"Mozilla Compatible Agent"}], "metrics"=>[{:pageviews=>60}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Mozilla%20Compatible%20Agent&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Opera", "dimensions"=>[{:browser=>"Opera"}], "metrics"=>[{:pageviews=>33}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Opera&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=BlackBerry9700", "dimensions"=>[{:browser=>"BlackBerry9700"}], "metrics"=>[{:pageviews=>8}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=BlackBerry9700&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=BlackBerry8900", "dimensions"=>[{:browser=>"BlackBerry8900"}], "metrics"=>[{:pageviews=>7}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=BlackBerry8900&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Mozilla", "dimensions"=>[{:browser=>"Mozilla"}], "metrics"=>[{:pageviews=>2}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Mozilla&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}, {"title"=>"ga:browser=Camino", "dimensions"=>[{:browser=>"Camino"}], "metrics"=>[{:pageviews=>1}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Camino&start-date=2010-02-27&end-date=2011-02-27", "updated"=>#}]

有沒有一種簡單的方法,使之成爲以壓平:

[2047,1196,835,227,60,33,8,7,2,1]

['Internet Explorer','Firefox','Chrome','Mozilla Compatible Agent','Opera','BlackBerry9700','Mozilla','Camino']

+0

這不會很容易閱讀... – BoltClock 2011-02-27 13:29:40

+0

有沒有一種簡單的方法來漂亮打印.inspect? – Tom 2011-02-27 13:40:59

+2

'require'pp';放置your_array.pretty_inspect'或'require'pp'; pp your_array' – 2011-02-27 14:54:21

回答

6

將您的陣列到data

pageviews = data.map{|d| d["metrics"][0][:pageviews]} #= [2047, 1196...] 

browsers = data.map{|d| d["dimensions"][0][:browser]} #= ['Internet Explorer', 'Firefox', ...] 
1

平展產生一個數組。不過,我想知道,它似乎是輸出,也許你真的想要一個具有數組作爲值的哈希?那是你在找什麼?

{ '標題'=> [ 「GA:瀏覽器=的Internet Explorer」, 「GA:瀏覽器=野生動物園」]}

如果是這樣,這樣的事情可能工作(有改編爲您的陣列):

newdata=arr.inject({}) do |memo,subhash| 
    subhash.each do |key,val| 
    memo[key] ||= [] 
    memo[key] << val 
    end 
    memo 
end 

puts newdata.inspect 

puts newdata['title'].inspect