Rory
Hi,
Been lurking here for a while (that is, via google), gotten a lot of help from people’s Q&As in the past, but I’ve gotten stuck on something that I don’t quite see an answer for in the documentation or the forums here. I feel like it’s something very simple, but I’m just not quite grokking how Access works.
I know how to access stuff in nested maps, but my question is specifically about stuff structured like this:
[ %StructFromAListOfStructs{ a_list_nested_in_that_struct: [ %{ some_attribute: "Blah blah blah" } ] }, ... ]
Each map/struct in this list has an attribute which returns a list of maps, and I want to access the keys in those nested lists.
Now if this was simply a nested map, I’d know what to do, but I get confused at this point. I’m sure if I studied the docs a little harder I could figure it out, but I figured I’d leverage the expertise here and hopefully save myself some time (thanks in advance!).
What I normally do in these sorts of situations is Enum.flat_map the list and then pipe into an Enum.map to access the attribute. But that’s two loops round what is (in my case) some very long lists. Is there a more efficient way to do this?
(BTW, this is not struct specific, just used that for convenience of self-commenting my example.)
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
gregvaughn
You likely want to use
Kernel.get_inwith some helpers from theAccessmodule such asAccess.atandAccess.key. Your example is a bit too vague to offer any concrete example, but the hexdocs of those functions should get you going.hst337
You could use
get_inorAccessas @gregvaughn said, but you’ll definitely come across some problems with structures and list traversal.The best solution is to use library called Pathex. It works with structures better than
Accessdoes, plus it is more efficient.So, with Pathex, your code for accessing this data would like this
Eiji
@Rory You can use a nested
reducecalls. The actual code would be different in each specific use case. For example if you want to takesoonestdate from such a nested data then you do not need to collect all elements and simply work on one single data, for example:Look how simple it’s to change. If we would like to take the opposite date then we all we need to do is to change 2 letters i.e.
:ltto:gt(when comparingdatewithacc). If you want to return multiple dates in this example then all you need to do is to change a defaultaccfromnilto[](empty list) and replace two last nestedreduceclauses to:With nested
reduceor alternatively simplepattern matchingthe whole list is iterated only once. See Enum.reduce/3 for more information.