How to extract the data out of enum

How to filter below enumerable which has value more than one ?

[
  {"12130", [%{id: 1410, val: "12130"}, %{id: 1414, val: "12130"}]},
  {"65130", [%{id: 1385, val: "65130"}]},
  {"65180", [%{id: 1386, val: "65180"}]}
]

On the example above 12130 should be the one that filtered

Hey Yudy, what have you tried so far?

1 Like

@benwilson512
I am getting that enum from group_by of ecto result as below

result |> Enum.group_by(fn x → x.val end )

I have tried to chain that group_by result like below

|> Enum.filter(fn {x, y} -> Enum.count(y) > 1 end) or below code
|> Enum.filter(fn %{x: y} -> Enum.count(y) > 1 end) or below code
|> Enum.filter(fn x, y -> Enum.count(y) > 1 end) or below code
|> Enum.filter(fn x, [y] -> Enum.count(y) > 1 end)

etc … which combine {}, [], %.

Basically I don’t know what data type this is called, that is why I am having difficulty to use correct formula.

Gotcha.

What you are looking at is a list of tuples, so you can do it this way:

|> Enum.filter(fn {_, list} -> length(list) >= 2 end)
2 Likes

@benwilson512 yes you are absolutely right. It’s working now. Thank you very much you save my day :grinning:

1 Like