What is the better way to use multi loop in elixir?

Hello, I have searched about using multi loop in one loop but I couldn’t find a good Post, I have 2 map and I want to use in one loop like this:

for card <- @cards do 
  for cat <- @categories do 
    if card.wedding_card_category_id === cat.id do 
      cat.title 
    end 
  end 
end 

I used this way that I wrote top, is it true way ? or it maybe better and has a different way?

Thanks

1 Like
for card <- @cards,
    cat <- @categories,
    card.wedding_card_category_id === cat.id,
    do: cat.title
5 Likes

I have tested it , but I have a problem

see my code please:

<!-- Loop of categories items -->
        <%= for card <- @cards, cat <- @categories do %>
          <tr>
            <td>
              <a href="/card-admin/card/category/<%= card.id %>"><%= card.title %></a>
            </td>
            <td>
              <div class="media align-items-center">
                <div class="media-body lh-1">

                      <%= if card.wedding_card_category_id === cat.id do %>
                        <%= cat.title %>
                      <% end %>

                </div>
              </div>
            </td>
            <td>
              <div class="media align-items-center">
                <div class="media-body lh-1">
                  <%= card.code %>
                </div>
              </div>
            </td>
            <td>
              <% time_need = WeddingCard.Extera.TimeCreator.jalali_create(card.inserted_at) %>
              <%= time_need.day_number %> <%= time_need.month_name %> <%= time_need.year_number %>
            </td>
            <td>
              <% time_need = WeddingCard.Extera.TimeCreator.jalali_create(card.updated_at) %>
              <%= time_need.day_number %> <%= time_need.month_name %> <%= time_need.year_number %>
            </td>
          </tr>
          <% end %>
          <!-- end of Loop of categories items -->
          <% end %>

It loops more than I need:

I’m afraid that I made a mistake, I edited this

        <%= for card <- @cards, cat <- @categories, card.wedding_card_category_id === cat.id do %>

instead of that code, I think it fix

3 Likes