展平 Ruby 数组

发布于 2024-10-19 13:55:36 字数 3079 浏览 4 评论 0原文

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

[{"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", "已更新"=>#}, {"title"=>"ga:browser=Safari", "dimensions"=>[{:browser= >“Safari”}]、“指标”=>[{: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", "更新"=>#}, {"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", "已更新" =>#}, {"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", "已更新"=>#}, {"title"=>"ga:browser=Mozilla 兼容代理", "dimensions"=>[{:browser= >“Mozilla 兼容代理”}]、“指标”=>[{:pageviews=>60}]、“id”=>“http://www.google.com/analytics/feeds/data? ids=ga:12316&ga:浏览器=Mozilla%20Compatible%20Agent&start-date=2010-02-27&end-date=2011-02-27", "已更新"=>#}, {"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", "已更新"=>#}, {"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", "已更新"=>#}, {"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", "更新"=>#}, {"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” , "已更新"=>#}, {"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 兼容代理','Opera','BlackBerry9700 ','Mozilla','卡米诺']

I have an Array which looks like this.

[{"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"=>#}]

Is there a simple way to flatten it so that it becomes:

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

and also

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疑心病 2024-10-26 13:55:36

将数组分配给data

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

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

Assign your Array to data

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

browsers = data.map{|d| d["dimensions"][0][:browser]} #= ['Internet Explorer', 'Firefox', ...]
终难愈 2024-10-26 13:55:36

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

Flatten produces an Array. I am wondering, though, it seems like for output maybe you really want a hash that has arrays as the values? Is that what you are looking for?

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

If so, something like this might work (with arr being your array):

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文