zhanjingbaobao
Merging two lists of maps
I currently have two list data.
data1 = [%{id: "111", name: "name1"},
%{id: "222", name: "name2"},
%{id: "333", name: "name3"},
%{id: "444", name: "name4"}
]
data2 = [
%{
mapData: %{
"111": "data1.com",
"222": "data2.com",
"333": "data3.com",
}
},
%{
mapData: %{
"111": "data11.com",
"222": "data22.com",
},
mapData: %{
"111": "data111.com"
}
}
]
Now I want to obtain data in this format.
[
%{id: "111", name: "name1",address:"data1.com"},
%{id: "111", name: "name1",address:"data11.com"},
%{id: "111", name: "name1",address:"data111.com"},
%{id: "222", name: "name2",address:"data2.com"},
%{id: "222", name: "name22",address:"data22.com"},
%{id: "333", name: "name3",address:"data3com"},
%{id: "444", name: "name4",address:""}
]
I don’t know how to implement it now.I am a beginner and I hope everyone can help me. Thank you.
Trending in Questions
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
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
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
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
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 12 Posts!
cloudytoday
Could you please post the sample data exactly the way you have it? From this example it’s not clear why in data2 the keys are quoted atoms (
"111": ...) and not usual string keys ("111" => ...) and whether this is intentional.Regardless, you could look for something like this:
dimitarvp
It will help if you give little more context about what exactly do you want to achieve. That way people can give you better solutions.
cloudytoday
So you want to match everything from data1 with the
namefield from data2, but how exactly? They don’t seem to have anything in common, so nothing to refer to. Or do you want to just match them by the order in the lists?Eiji
Copying these over from the Elixir chat room.
[chat quote=“zhanjingbaobao;869;2023-04-10T07:37:20Z” channel=“Elixir Chat” channelId=“2” multiQuote=“true” chained=“true”]
I currently have two list data.
I don’t know how to implement it now.I am a beginner and I hope everyone can help me. Thank you.
[/chat]
[chat quote=“Eiji;871;2023-04-10T10:02:12Z” chained=“true”]
Your
data2map is invalid as it’s keys are not unique and therefore only last would be used.There are 2 other issues … In data2 the first
111havedata1address, but should havedata1_test. InmergeDatayou havename22instead ofname2.After applying all fixed this is a final example code:
[/chat]
[chat quote=“zhanjingbaobao;880;2023-04-11T07:33:27Z” chained=“true”]
Thank you very much Eiji,but when I tested the code, I didn’t get the data structure I expected.
I have two pieces of data:
data1anddata2,Here are my expected data.Expected results do not need to be sorted, as long as the content can be aligned.
Thank you for reading this message.
[/chat]
[chat quote=“Eiji;882;2023-04-11T08:59:52Z” chained=“true”]
Again, you had issues in your data. Firstly there is
descri**b**tioninstead ofdescri**p**tion. Secondly 2nd last expected map should have nametestName1instead oftestName2.Here goes the complete example with fixed data:
@AstonJ Could you please move our messages to a separate topic, please?
[/chat]
Note: Fixed code formatting.
adamu
Reminds me of this How to merge two list with maps based on particular key
And the person on SO asking the same thing. Just has mapData instead of price.
zhanjingbaobao
When I verified the code, I found that true could not be printed out, and the print result:
zhanjingbaobao
I use the channelID of data1 to match any key of mediaTailoId in data2. If the match is successful, data1 will add a new piece of data. The data of id=1 in data1 appears three times in data2, so there are three id=1 in the final result I generated, and the name of each is the name of data2. id=4 does not have any matching records in data2, so name=“”.My code,but it doeson’t work.
Eiji
No idea how you got those results. Which code example have you used? Since you give 2 different inputs and expected I wrote two examples. Maybe you have used the wrong one?
zhanjingbaobao
If there is a piece of data in data2 that does not match the channelId of data1, there will be an exception. I tried to judge but failed.
For example:
data1 has not changed,I think the problem should be here, but I failed to modify it successfully, I am very sorry to ask you for help again.
Eiji
Yes, as this was not mentioned originally.
No worries.
No, it’s not. As above you have added a special cases not mentioned before. Here is the current error:
We can see that we have done with
data, but still have extranameslist elements. This means that we only need to modify a strict condition that finishes our recursive process i.e.Also I have simplified my example a bit:
Last Post!
zhanjingbaobao
sorry,this is the new data
On this basis I also want to get the name attribute in data2 as another attribute of data1. I tried without success. Do you have any good suggestions?
my expect_data