Ruby:从数组中选择一个散列 [英] Ruby: select a hash from inside an array
本文介绍了Ruby:从数组中选择一个散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数组:
response = [{"label"=>"cat", "name"=>"kitty", "id"=>189955}, {"label" => "dog", "name"=>"rex", "id" => 550081}]
如何选择包含标签 cat 的哈希?我知道 response.first
会给我相同的结果,但我想按标签搜索.
How do I select the hash that contains the label cat? I know response.first
will give me the same result, but I want to search the by label.
谢谢!
黛布
推荐答案
response.find {|x| x['label'] == 'cat' } #=> {"label"=>"cat", "name"=>"kitty", "id"=>189955}
这篇关于Ruby:从数组中选择一个散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文