展平 Ruby 数组
我有一个看起来像这样的数组。
[{"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将数组分配给
data
Assign your Array to
data
Flatten 产生一个数组。不过,我想知道,对于输出来说,您似乎真的想要一个以数组作为值的哈希?这就是您要找的吗?
{'title'=>["ga:browser=Internet Explorer", "ga:browser=Safari"]}
如果是这样,类似这样的事情可能会起作用(以 arr 为数组):
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):