I have a list of structs from the return of Repo.preload. And then I have a map that has a list with items in it.
structs = [%Struct{...}, %Struct{...}]
{:ok, states} = %{states: [ %{ "pos_x" => 1.0, "pos_y" => 2.0, "pos_z" => 1.022, "vel_x" => 2.01, "vel_y" => 12.0, "vel_z" => 129.0 }, %{ "pos_x" => 10000.0, "pos_y" => -2.0, "pos_z" => -1.022, "vel_x" => -2.01, "vel_y" => -12.0, "vel_z" => -129.0 } ]}
My goal is to be able to take item one from the states
map and put it in the first struct. And then take item two from the states
map and put it in the second struct. And so on and so on. The amount of structs will always match the amount of states.
I have tried list = Enum.map(structs, fn s -> Enum.map(states, fn x -> Map.merge(s, x) end) end)
but obviously this duplicates the structs list that is returned.
I have also tried making states
a list of maps instead, therefore having two lists, states
, and structs
. And then doing a simple Enum.zip(structs, states)
. This returns me a list of tuples, which cannot be used in the Ecto schema.
Any help/guidance on this?