如果我有:
[
%{group: "123", name: "John"},
%{group: "123", name: "Jane"},
%{group: "123", name: "James"},
]
我如何将其转换为:
%{
group: "123",
members: [%{name: "John"}, %{name: "Jane"}, %{name: "James"}]
}
我尝试:
from m in Module.Member,
group_by: m.group,
... #with a few different queries...
但刚刚了解到group_by不是这样工作的
根据您想要的通用性级别,这可能就足够了:
data = [
%{group: "123", name: "John"},
%{group: "123", name: "Jane"},
%{group: "123", name: "James"},
]
members = Enum.map(data, fn p -> %{name: p.name} end)
result = %{group: Enum.at(data, 0).group, members: members}
这不会处理数据中不同组号的情况。