无法筛选散列数组,因为键没有属性

bttbmeg0  于 2022-10-15  发布在  Ruby
关注(0)|答案(1)|浏览(192)

我有这个哈希

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方法,但不起作用…

mwg9r5ms

mwg9r5ms1#

如果只需要删除["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}]

但正如我在问题中看到的,您还可以尝试Map过滤数据(使用符号键),在本例中,您可以使用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"}}]

相关问题