filter enum by map keyword not working

I have this list

 data = [
  %{
    "a" => ["x", "y", "z"],
    "b" => 11,
    "c" => 100,
    "d" => 20,
    "e" => true
  },
  %{
    "a" => ["w"],
    "b" => 3,
    "c" => 200,
    "d" => 20,
    "e" => false
  }
]

How do I get Enum.filter to work to return the map where state == true?

This return nil

data |> Enum.filter(fn {_, v} -> Keyword.get(v, :e) == true end)

Hi,

data is a list of maps, not a keyword-list and you used the atom :e when your key is the string "e"

data
|> Enum.filter(&(match?(%{"e" => true}, &1)))

?

1 Like

When I run that code locally with the data above it, this is what I get:

iex(2)> data |> Enum.filter(fn {_, v} -> Keyword.get(v, :e) == true end)
** (FunctionClauseError) no function clause matching in :erl_eval."-inside-an-interpreted-fun-"/1    
    
    The following arguments were given to :erl_eval."-inside-an-interpreted-fun-"/1:
    
        # 1
        %{"a" => ["x", "y", "z"], "b" => 11, "c" => 100, "d" => 20, "e" => true}
    
    (stdlib 3.16.1) :erl_eval."-inside-an-interpreted-fun-"/1
    (stdlib 3.16.1) erl_eval.erl:834: :erl_eval.eval_fun/6
    (elixir 1.13.0) lib/enum.ex:4033: Enum.filter_list/2

Since data is list of maps, the function given to Enum.filter will be called with a map.

Enum.filter(data, fn m -> m["e"] end)

Thank you for the clarification,it works

I now started understanding the list of maps better -

Now Passing the filter this enum of two items

data = [
  %{
    "a" => ["x", "y", "z"],
    "b" => 11,
    "c" => 100,
    "d" => 20,
    "e" => true
  },
  %{
    "a" => ["w"],
    "b" => 3,
    "c" => 200,
    "d" => 20,
    "e" => true
  }

]

the vector w matches the size of data

w = [ u, v] 

I want to override “d” with list w
I know this doesn’t work as it copies the entire w into each data enum
There is that trick of recursion that I can’t fully understand yet

t = Enum.map(&Map.delete(&1, "d"))|> Enum.map(&Map.put(&1, "d", w ))

how to make into

data = [
  %{
    "a" => ["x", "y", "z"],
    "b" => 11,
    "c" => 100,
    "d" => u,
    "e" => true
  },
  %{
    "a" => ["w"],
    "b" => 3,
    "c" => 200,
    "d" => v
    "e" => true
  }

]

Hey @EduardoC as a minor note, please use good forum etiquette and put code blocks around your code so that your posts are easier to read and don’t have to be edited by the mods.

As far as your question goes, check out Enum — Elixir v1.16.3

taking notes, thanks!