我有这个哈希
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
}但是关于最后一个值“属性”=>nil,当然还有
undefined method `dig' for nil:NilClass我也尝试用try方法,但不起作用.
发布于 2022-10-14 09:44:03
如果只需要删除["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://stackoverflow.com/questions/74066831
复制相似问题