我正在構建一個 rails 5.2 應用程式。
在這個應用程式中,我需要將一個物件轉換為一個陣列,但只保留獲得真值的屬性(在本例中,notification_email)
物件是這樣的:
{"notification_email"=>true, "notification_alert"=>false}
我想實作這一目標:
["notification_email"]
uj5u.com熱心網友回復:
x = {"notification_email"=>true, "notification_alert"=>false}
結果 = x.delete_if {|x,y| y == false}.keys
[“notification_email”]
uj5u.com熱心網友回復:
hash = {"notification_email"=>true, "notification_alert"=>false} 如果我們想找出資料,那么我們可以使用 select
try=> hash.select{|x,y| y == true}.keys
結果:[“notification_email”]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376643.html
標籤:红宝石轨道
