Remove fields from nested struct

I have this struct of preload records.
I want to remove
__meta__ and Ecto.Association.NotLoaded from the main struct as well as from the nested array of preloaded records. To make it a simple map

  %Facility{
  __meta__: #Ecto.Schema.Metadata<:loaded, "facilities">,
  id: 4875,
  instance: #Ecto.Association.NotLoaded<association :instance is not loaded>,
  name: "Facility",
 units: [
%Unit{
  __meta__: #Ecto.Schema.Metadata<:loaded, "units">,
  facility: #Ecto.Association.NotLoaded<association :facility is not loaded>,
  facility_id: 4875,
  id: 1593,
  name: "Storage",
  rooms: #Ecto.Association.NotLoaded<association :rooms is not loaded>
},
%Unit{
  __meta__: #Ecto.Schema.Metadata<:loaded, "units">,
  cdc_location_class_id: nil,
  facility: #Ecto.Association.NotLoaded<association :facility is not loaded>,
  facility_id: 4875,
  id: 1594,
  name: "Storage unit",
  rooms: #Ecto.Association.NotLoaded<association :rooms is not loaded>
}
],
users: #Ecto.Association.NotLoaded<association :users is not loaded>
}

Is it possible?

Thanks

Is it possible?

Yes, try writing a recursive function.

1 Like

Everything is about transforming data… Another way is to use Enum and Map modules :slight_smile:

iex> h Enum.
EmptyError           OutOfBoundsError     all?/1               
all?/2               any?/1               any?/2               
at/2                 at/3                 chunk_by/2           
chunk_every/2        chunk_every/3        chunk_every/4        
chunk_while/4        concat/1             concat/2             
count/1              count/2              dedup/1              
dedup_by/2           drop/2               drop_every/2         
drop_while/2         each/2               empty?/1             
fetch!/2             fetch/2              filter/2             
find/2               find/3               find_index/2         
find_value/2         find_value/3         flat_map/2           
flat_map_reduce/3    group_by/2           group_by/3           
intersperse/2        into/2               into/3               
join/1               join/2               map/2                
map_every/3          map_join/2           map_join/3           
map_reduce/3         max/1                max/2                
max_by/2             max_by/3             member?/2            
min/1                min/2                min_by/2             
min_by/3             min_max/1            min_max/2            
min_max_by/2         min_max_by/3         random/1             
reduce/2             reduce/3             reduce_while/3       
reject/2             reverse/1            reverse/2            
reverse_slice/3      scan/2               scan/3               
shuffle/1            slice/2              slice/3              
sort/1               sort/2               sort_by/2            
sort_by/3            split/2              split_while/2        
split_with/2         sum/1                take/2               
take_every/2         take_random/2        take_while/2         
to_list/1            uniq/1               uniq_by/2            
unzip/1              with_index/1         with_index/2         
zip/1                zip/2                

And Map

iex> h Map.       
delete/2             drop/2               equal?/2             
fetch!/2             fetch/2              from_struct/1        
get/2                get/3                get_and_update!/3    
get_and_update/3     get_lazy/3           has_key?/2           
keys/1               merge/2              merge/3              
new/0                new/1                new/2                
pop/2                pop/3                pop_lazy/3           
put/3                put_new/3            put_new_lazy/3       
replace!/3           split/2              take/2               
to_list/1            update!/3            update/4             
values/1

hey thanks for your input. I have one question is it also possible to add list of maps to a map in a specific order:

Like these two:

                    params = %{name: "john wick"}
                    list_of_maps = [%{id: 1}, %{id: 2}, %{id: 3}]

            result = %{name: "john wick", some_key: list_of_maps}

When i try to add them the order is reversed. I know its off topic just curious :slightly_smiling_face:

Can you suggest alternate appraoch?
Thanks

Can you suggest alternate appraoch?

Add them in the reversed order? I’m not sure what you are doing, though. Where do the maps come from?

If i added them in the reverse order then it should be a list. i want it to be a map. Params come from the function. And I want it to manipulate it and add list of items in it like the example above…

This should not reverse anything, could you please be more specific about the problem?

Params
|> Map.put(:key, list_of_maps)
The output is

      %{ key: [%{id:1}, ....], name: "john wick"}

What I want is this:

      %{ name: "john wick", key: [%{id:1}, ....] }

These outputs are identical. Try comparing them with === if you are in doubt.

1 Like

There is no difference in both, those are equivalent from elixirs point of view.

Keys in a map are without any ordering guarantees.

1 Like

Thanks

Thank you