Ruby: select a hash from inside an array

后端 未结 2 1583
臣服心动
臣服心动 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:11

    Try:

    response.select { |x| x["label"] == "cat" }
    
    0 讨论(0)
  • 2021-01-30 18:31
    response.find {|x| x['label'] == 'cat' } #=> {"label"=>"cat", "name"=>"kitty", "id"=>189955}
    
    0 讨论(0)
提交回复
热议问题