【发布时间】:2015-08-18 21:01:16
【问题描述】:
我在尝试从数组中获取对象时遇到问题,其中给定的散列可能具有特定属性和特定值,或者嵌套散列也可能具有。
有没有一种方法可以返回具有我需要的密钥的特定哈希,或者当它没有时返回?
示例:我有这个完全虚构的结构:
the_array = [
{
:is_father => true,
:seek_this => "01"
},
{
:is_uncle => false,
:children => [
{
:seek_this => "09"
},
{
:seek_this => "2a"
}
]
},
{
:random_property=> 3,
:children => {
:random_er => true,
:children => [
{
:is_father => false,
:children => [
{
:seek_this => "3b"
},
{
:seek_this => "h1"
}
]
}
]
}
}
]
调用后
the_array
.methodThatIDoNotKnow { |x| !x.seek_this.nil? }
.each do |hash_i_need|
//operate on hash somehow
hash_i_need.seek_this = 0xDEADBEEF
end
这是我所期望的:
the_array = [
{
:some_key => true,
:seek_this => 0xDEADBEEF
},
{
:some_other_key => false,
:children => [
{
:seek_this => 0xDEADBEEF
},
{
:seek_this => 0xDEADBEEF
}
]
},
{
:random_key: 3,
:children => {
:random_er => true,
:children => [
{
:is_father => false,
:children => [
{
:seek_this => 0xDEADBEEF
},
{
:seek_this => 0xDEADBEEF
}
]
}
]
}
}
]
我知道这是我可以自己编码的东西,我只是想知道我是否需要或者是否有开箱即用的这种搜索功能。
谢谢!
【问题讨论】: