我有以下数组:
response = [{"label"=>"cat", "name"=>"kitty", "id"=>189955}, {"label" => "dog", "name"=>"rex", "id" => 550081}]
如何选择包含标签“cat”的哈希?我知道response.first
将给我相同的结果,但我想按标签搜索。谢谢!
Deb
我有以下数组:
response = [{"label"=>"cat", "name"=>"kitty", "id"=>189955}, {"label" => "dog", "name"=>"rex", "id" => 550081}]
如何选择包含标签“cat”的哈希?我知道response.first
将给我相同的结果,但我想按标签搜索。
Array#find
返回第一个匹配项或nil,而Array#select
和Array#find_all
返回所有匹配元素的数组。 - ZargonyArray#find
的一个同义词是Array#detect
。 - PreciousBodilyFluids