I'm trying to end up with a single map containing many different preferences, it should look like this:
%{some_preference_name:%{foo:"bar"},another_preference_name:%{foo:"bar"}}
I have a list of preference maps from the database, and I need to get through them and set the "preference" field as the key with the various values as the values map.
I tried to do this with Enum.reduce and Enum,map, but I can't get the list right.
Enum.map(preferences, fn(data)->
Map.put(%{}, data.preference,
%{
foo: data.foo
}
)
end)
returns:
[{some_preference_name:%{foo:"bar"}},{another_preference_name:%{foo:"bar"}}]
then:
Enum.reduce(preferences, fn(acc, data)->
Map.put(acc, data.preference,
%{
foo: data.foo
}
)
end)
returns:
%{some_preference_name:%{foo:"bar"},preference: "another_preference_name",foo:"bar"}
It gets the first one right, but not the rest. I understand that as of Erlang R17, the only way I will be able to add a variable key name is with Map.put/3.