script

script OP

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

First 10 of 11 Posts Switch mode

idi527

idi527

Is it possible?

Yes, try writing a recursive function.

kokolegorille

kokolegorille

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
script

script OP

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

idi527

idi527

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?

script

script OP

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…

NobbZ

NobbZ

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

script

script OP

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}, ....] }
idi527

idi527

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

NobbZ

NobbZ

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

Keys in a map are without any ordering guarantees.

script

script OP

Thanks

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

ElixirConf
Lightning Talk: Jacob Swanner - Put your Scaller (Named Correctly) | ElixirConf US 2025 https://www.youtube.com/watch?v=dqqZSSxs-MA Co...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement