簡體   English   中英

Ruby:從數組中選擇一個哈希

[英]Ruby: select a hash from inside an array

我有以下數組:

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

如何選擇包含標簽cat的哈希? 我知道response.first會給我相同的結果,但我想搜索by標簽。

謝謝!

德布

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

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM