展平Ruby数组

时间:2011-02-27 13:23:14

标签: ruby arrays hash

我有一个看起来像这样的数组。

[{"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']

2 个答案:

答案 0 :(得分:6)

将数组分配到data

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

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

答案 1 :(得分:1)

Flatten生成一个数组。我想知道,但是,似乎输出可能你真的想要一个有数组作为值的哈希?那是你在找什么?

{'title'=> [“ga:browser = Internet Explorer”,“ga:browser = Safari”]}

如果是这样,这样的事情可能会起作用(arr是你的数组):

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