Iterating through an array of articles where each article could have it‘s own individual seller

Hey Folks,

imagine you have a “shopping cart” full of items. Each item has a unique “SellerID”.

Now I would like to create an order in the database PER sellerID.

That means I would have to split the shopping cart, basically an array full of items, into individual arrays, split by the respective sellerID.

Sure I can iterate over the sellerIDs and could, "if sellerID == item.sellerID populate some other array, but that’s where I’m stuck right now. :sweat_smile:

With the number of individual sellerIDs within the cart, I could declare arrays before iterating on each, but that doesn’t seem pragmatic to me.

I’m also more about the principle and whether there is a pragmatic solution to this problem. :joy:

PS have personally already decided that I rewrite the shopping cart, so that there is already ordered from the beginning by seller ID, then I can create the order directly iterate over the individual seller IDs of the cart.

Still, I’m interested if there is a solution-oriented way if I haven’t already sorted it by SellerID in the cart before.

So thanks in advance for thinking about it! :slight_smile:

Could you post us an example of the list you have and an example of how you want to end up?

Enum.group_by should do the trick…

4 Likes

My data comes in like that:

[
%{id: 1, seller_id: 3, title: "Article1", price: 33.22, etc.},
%{id: 2, seller_id: 2, title: "Article1", price: 33.22, etc.},
%{id: 3, seller_id: 3, title: "Article1", price: 33.22, etc.}
]

and what I’d need is something like this:

[
  %{3, [
    %{id: 1, seller_id: 3, title: "Article1", price: 33.22, etc.},
    %{id: 3, seller_id: 3, title: "Article1", price: 33.22, etc.}
  ]},
  %{2, [
    %{id: 2, seller_id: 2, title: "Article1", price: 33.22, etc.}
  ]}
]

As mentioned by kokolegorille, Enum.group_by will do the trick. I just tried it with:

Enum.group_by(articles, & &1.seller_id)

And what I get is:

%{
  2 => [
              %{id: 2, seller_id: 2, title: "Article1", price: 33.22, etc.}
          ],
  3 => [
              %{id: 1, seller_id: 3, title: "Article1", price: 33.22, etc.},
              %{id: 3, seller_id: 3, title: "Article1", price: 33.22, etc.}
          ]
  }

So exactly what I want, THANKS! :slight_smile:

But how would I solve this if Enum.group_by did not exist , or if I used a completely different language, e.g. an object-oriented programming language like Java?

I am just interested in how this would be implemented, and I do not think that it’s straightforward for me looking into the Enum.group_by implementation for that. :smiley:

Many Greets, Franz. :slight_smile:

Under the hood, it is using a reduce… which is available in FP (might be called differently, like fold, foldl)

Enum.reduce(articles, %{}, fn x, acc -> Map.update(acc...) end)

In OO, You could probably do something like this pseudo code.

result = ...
articles.foreach(
 ...
  result[] = ...
)
3 Likes