我有這個哈希
question["options"]
=> [{"id"=>10217, "title"=>{"English"=>"Da A a E"}, "value"=>"Da A a E", "properties"=>{"disabled"=>false}},
{"id"=>10218, "title"=>{"English"=>"Profondo"}, "value"=>"Profondo", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10219, "title"=>{"English"=>"La macchina da caffè a capsule"}, "value"=>"La macchina da caffè a capsule", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10234, "title"=>{"English"=>"Extender Wifi"}, "value"=>"Extender Wifi", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10246, "title"=>{"English"=>"Da A a F"}, "value"=>"Da A a F", "properties"=>{"disabled"=>false}},
{"id"=>10247, "title"=>{"English"=>"Sistematico"}, "value"=>"Sistematico", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10250, "title"=>{"English"=>"4"}, "value"=>"4", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10252, "title"=>{"English"=>"Ruote"}, "value"=>"Ruote", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10268, "title"=>{"English"=>"Sub 8 GHz"}, "value"=>"Sub 8 GHz", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10273, "title"=>{"English"=>"La copertura sul territorio"}, "value"=>"La copertura sul territorio", "properties"=>{"disabled"=>true, "piping_exclude"=>"false", "show_rules"=>nil, "show_rules_logic_map"=>nil}},
{"id"=>10280, "title"=>{"English"=>"Da A a G"}, "value"=>"Da A a G", "properties"=>nil}]
我需要過濾這個陣列,洗掉每個值["property"]["disabled"] is true
我正在使用這個
question["options"].filter{ |o|
unless o["properties"].dig("disabled")
{
"value": o["value"],
"id": o["id"],
"text": {
"it": o["title"]["English"]
}
}
end
}
但是在最后一個值“properties”=>nil,當然
undefined method `dig' for nil:NilClass
我也嘗試使用try方法,但不起作用......
uj5u.com熱心網友回復:
如果您只需要洗掉元素,["properties"]["disabled"]您true只需要Array#reject在這種情況下使用
question["options"].reject { |o| o.dig("properties", "disabled") }
# => [{"id"=>10217, "title"=>{"English"=>"Da A a E"}, "value"=>"Da A a E", "properties"=>{"disabled"=>false}},
# {"id"=>10246, "title"=>{"English"=>"Da A a F"}, "value"=>"Da A a F", "properties"=>{"disabled"=>false}},
# {"id"=>10280, "title"=>{"English"=>"Da A a G"}, "value"=>"Da A a G", "properties"=>nil}]
但是正如我在問題中看到的那樣,您還嘗試映射過濾后的資料(使用符號鍵),在這種情況下您可以使用Enumerable#filter_map
question["options"].filter_map do |o|
unless o.dig("properties", "disabled")
{
value: o["value"],
id: o["id"],
text: { it: o["title"]["English"] }
}
end
end
# => [{:value=>"Da A a E", :id=>10217, :text=>{:it=>"Da A a E"}},
# {:value=>"Da A a F", :id=>10246, :text=>{:it=>"Da A a F"}},
# {:value=>"Da A a G", :id=>10280, :text=>{:it=>"Da A a G"}}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/516606.html
標籤:红宝石
