Retrieving information from a List of maps?

Enum.map(?

1 Like

Of course. Now show me a code snippet that’s doing it.

|> Enum.map(%{customer: map["value"]})

Have you tried running that in iex? I don’t want you to just write what you think it is, I want you to try and run it as well.

 |> Map.get(:body) |> Enum.map(%{customer: map["value"]})
** (BadFunctionError) expected a function, got: %{customer: "EOG.Acme.ManualXML"}
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2

Yep. Can’t work.

How well do you know Elixir?

1 Like

I have never really seen it since a couple of weeks maybe a bit longer but only a couple of weeks of really trying

Well that explains a lot.

Do you know what is the expected type of the 2nd argument to Enum.map?

a function

Yep. And %{customer: map["value"]} is not a function.

So what next?

Have you covered the official guide?

|> Enum.into( %{}, fn item -> {"Customer", item["value"]} end)

Yes, somewhat, but Enum.into only fills up a single map.

You want a list of maps as a result. That’s why you should be using Enum.map. You need that but you are giving it the wrong 2nd argument. It must be a function, not a map.

Do you know how to pass a function as an argument to another function?

1 Like

Kind of like the one above?

 |> Enum.map( fn item -> {"Customer", item["value"]} end)

which give me

[
  {"Customer", "EOG.Acme.ManualXML"},
  {"Customer",
   "{\"variables\":{\"CUSTOMER\":\"EOG.Acme.ManualXML\",\"VERSION\":\"Version-8.6.2\"},\"id\":\"5112\",\"ref\":\"main\"}"},
  {"Customer", "Version-8.6.2"}
]

You are almost there. :slight_smile:

It is exciting

1 Like

OK, put |> Map.new() at the end. :wink:

I do want

[
  {"Customer", "EOG.Acme.ManualXML"},
  {"Version", "Version-8.6.2"}
]

do I have to use some kind of if statement on item[“key”] = “CUSTOMER” etc.

No, this is a single record. If you put |> Map.new() at the end of the above processing you’ll get a single map. To have a list of maps / records then use Enum.map.

Look, I can give you the solution in a minute but IMO you should practice Elixir more, it’s really easy.

4 Likes

One tip, ask chatgpt how to do something in elixir, it can be your pair programmer who does nothing but knows everything. :slight_smile: