我有一個函式,它回傳一個陣列物件,我想回傳一個陣列中的 ID(因為我可以有很多物件),其中每個物件都有一個。這是我嘗試過的。
iban_obj.map{ |id| id },
iban_obj.select(&:id)
這是我的 iban_obj:
[{"url"=>"xxxx.json",
"id"=>360081391060,
"title"=>"Test Macro",
"active"=>true,
"updated_at"=>"2021-11-22T13:15:06Z",
"created_at"=>"2021-11-19T16:52:00Z",
"position"=>10002,
"description"=>"{{ iban }} {{ phone_number }} {{ email }}",
"actions"=>
[{"field"=>"comment_value_html",
"value"=>
"restriction"=>nil}]
預期結果:
id_array = [360081391060]
uj5u.com熱心網友回復:
id_array = iban_obj.map { |obj| obj['id'] }
當您呼叫時,#map每個整個物件都會被傳入。從那里您必須像從任何散列中一樣選擇正確的鍵。
uj5u.com熱心網友回復:
您也可以使用挖掘方法..
banking_customer.map{ |customer| customer.dig('id') }
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364013.html
