简体   繁体   中英

Ruby: select a hash from inside an array

I have the following array:

response = [{"label"=>"cat", "name"=>"kitty", "id"=>189955}, {"label" => "dog", "name"=>"rex", "id" => 550081}]

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.

Thanks!

Deb

response.find {|x| x['label'] == 'cat' } #=> {"label"=>"cat", "name"=>"kitty", "id"=>189955}

尝试:

response.select { |x| x["label"] == "cat" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM