Home > Software design >  Cannot filter an array of hashes because a key hasn't got an attribute
Cannot filter an array of hashes because a key hasn't got an attribute

Time:10-15

I have this hash

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}]

I need to filter this array removing every value where ["property"]["disabled"] is true

I'm using this

      question["options"].filter{ |o|
        unless o["properties"].dig("disabled")
          {
           "value": o["value"],
           "id": o["id"],
           "text": {
            "it": o["title"]["English"]
           }
          }
        end
      }

but on the last value "properties"=>nil and of course

undefined method `dig' for nil:NilClass

I try also with try method but is doesn't work...

CodePudding user response:

If you just need to remove elements where ["properties"]["disabled"] is true you just need use Array#reject with such condition

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}]

But as I see in the question, you also try to map filtered data (with symbol keys), in this case you can use 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"}}]
  •  Tags:  
  • ruby
  • Related