I have array of nested hash that is,
@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"}]
}]
}]
I need array of all values which have key name 'id'. like @b = [5,19,21,20,22,23] I have already try this '@a.find { |h| h['id']}`. Is anyone know how to get this?
Thanks.
@a.to_s.scan(/(?<=\"id\"=>\")\d+/).map(&:to_i) #=> [5, 19, 21, 20, 22, 23].