【发布时间】:2014-12-23 10:49:05
【问题描述】:
我有嵌套哈希数组,即,
@a = [{"id"=>"5", "head_id"=>nil,
"children"=>
[{"id"=>"19", "head_id"=>"5",
"children"=>
[{"id"=>"21", "head_id"=>"19", "children"=>[]}]},
{"id"=>"20", "head_id"=>"5",
"children"=>
[{"id"=>"22", "head_id"=>"20", "children"=>[]}, {"id"=>"23"}]
}]
}]
我需要键名为“id”的所有值的数组。像@b = [5,19,21,20,22,23] 我已经尝试过这个'@a.find { |h| h['id']}`。 有谁知道如何获得这个?
谢谢。
【问题讨论】:
-
这也有效:
@a.to_s.scan(/(?<=\"id\"=>\")\d+/).map(&:to_i) #=> [5, 19, 21, 20, 22, 23]. -
@CarySwoveland 太棒了它的工作就像魅力一样。谢谢
标签: ruby-on-rails ruby arrays hash