Ruby: select a hash from inside an array

后端 未结 2 1589
臣服心动
臣服心动 2021-01-30 17:49

I have the following array:

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


        
2条回答
  •  清酒与你
    2021-01-30 18:31

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

提交回复
热议问题