tio407

tio407

Filtering a deeply nested map

Hello, I am trying to filter a deeply nested map like the one below. I want to filter for the case where two of the fields match a certain condition. I’ve also included what my query currently looks like. It is wrong because it returns the same element twice.

  %Accounts.User{
    __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
    accounts: [
      %Accounts.Account{
        __meta__: #Ecto.Schema.Metadata<:loaded, "accounts">,
        id: 1,
        inserted_at: ~U[2020-03-13 19:55:01Z],
        account_items: [
          %AccountItem{
            __meta__: #Ecto.Schema.Metadata<:loaded, "plaid_items">,
            account_id: 1,
            id: 2,
            inserted_at: ~U[2020-03-13 19:55:03Z],
            updated_at: ~U[2020-03-13 19:55:03Z],
            user_notified_at: nil,
            notification_sent_at: nil,
            status: "sample_status_here"
          },
                   %AccountItem{
            __meta__: #Ecto.Schema.Metadata<:loaded, "plaid_items">,
            account_id: 1,
            id: 2,
            inserted_at: ~U[2020-03-13 19:55:03Z],
            updated_at: ~U[2020-03-13 19:55:03Z],
            user_notified_at: nil,
            notification_sent_at: nil,
            status: "sample_status_here"
          }
        ],
      }
    ],
    id: 1,
    inserted_at: ~U[2020-03-13 19:55:01Z],
  },
  %Accounts.User{
    __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
    accounts: [
      %Accounts.Account{
        __meta__: #Ecto.Schema.Metadata<:loaded, "accounts">,
        id: 1,
        inserted_at: ~U[2020-03-13 19:55:01Z],
        account_items: [
          %AccountItem{
            __meta__: #Ecto.Schema.Metadata<:loaded, "plaid_items">,
            account_id: 1,
            id: 2,
            inserted_at: ~U[2020-03-13 19:55:03Z],
            updated_at: ~U[2020-03-13 19:55:03Z],
            user_notified_at: nil,
            notification_sent_at: nil,
            status: "sample_status_here"
          },
          %AccountItem{
            __meta__: #Ecto.Schema.Metadata<:loaded, "plaid_items">,
            account_id: 1,
            id: 2,
            inserted_at: ~U[2020-03-13 19:55:03Z],
            updated_at: ~U[2020-03-13 19:55:03Z],
            user_notified_at: nil,
            notification_sent_at: nil,
            status: "sample_status_here"
          }
        ],
      }
    ],
    id: 1,
    inserted_at: ~U[2020-03-13 19:55:01Z],
  },
]

This is my current function:

   Enum.flat_map(map, fn user ->
      Enum.flat_map(user.accounts, fn account ->
        Enum.filter(
          account.account_items,
          fn item ->
            item.status == "pending" and
              item.notification_sent_at == nil
          end
        )
      end)
    end)

I expect to get all of the items that fulfill the criteria of

item.status == "pending" and
              item.notification_sent_at == nil

Most Liked Responses

gregvaughn

gregvaughn

I haven’t actually executed this because I’d have to create struct modules to do it, but I’d be looking at something like:

for user <- list_of_users,
  account <- user.accounts,
  %{status: "pending", notification_sent_at: nil} = item <- account.account_items,
  do: item

edit: add newlines into the code because I hate how the forum software (plus Safari?) render long single line codeblocks. The scroll bar hides the content

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement