Hi guys, I need your help.
I have data like this
[
%Product{name: "product 1", category: "Food"},
%Product{name: "product 2", category: "Food"},
%Product{name: "product 3", category: "Food"},
%Product{name: "product 4", category: "Drink"},
%Product{name: "product 5", category: "Drink"},
%Product{name: "product 6", category: "Drink"}
]
Can I transform this data to like this?
[
%{
name: "Food",
products: [
%Product{name: "product 1", category: "Food"},
%Product{name: "product 2", category: "Food"},
%Product{name: "product 3", category: "Food"},
],
},
%{
name: "Drink",
products: [
%Product{name: "product 4", category: "Drink"},
%Product{name: "product 5", category: "Drink"},
%Product{name: "product 6", category: "Drink"}
]
}
]
I tried to use Enum.group_by and Enum.into But I can’t make it work.
It is grouped by category and if it has same category, then put that product in products list.
How can I do this?