billposters
Formatting Lists of Maps
I’ve a data source that’s returning some time series data that I’d like to ultimately chart in Phoenix. However I’m having some trouble massaging it into some format or another that I can use.
What I’m starting with is along the lines of:
[
%{x: 1570117280000, y: 16},
%{x: 1570117288000, y: 16},
%{x: 1570117296000, y: 16},
%{x: 1570117304000, y: 16}
]
I’ve been able to render a graph by doing:
stats
|> Enum.map(&Map.to_list/1)
|> Enum.map(&Keyword.values/1)
|> Jason.encode!()
And then passing it through the controller to the template, which gives me:
[
[1570117280000, 16],
[1570117288000, 16],
[1570117296000, 16],
[1570117304000, 16]
]
But I’m struggling with how to format the key values. For example, converting the unix timestamp x to something more friendly.
How do I manipulate a specific key’s value when I’m going through the list?
Marked As Solved
albydarned
Is this what you’re looking for?
[
%{x: 1570117280000, y: 16},
%{x: 1570117288000, y: 16},
%{x: 1570117296000, y: 16},
%{x: 1570117304000, y: 16}
]
|> Enum.map(fn(%{x: x, y: y}) -> [DateTime.from_unix!(x, :millisecond), y] end)
|> Jason.encode!()
3
Also Liked
kokolegorille
It is how You represented the result in the first post… and it is not a map, but a list.
An alternative…
iex> list = [
%{x: 1570117280000, y: 16},
%{x: 1570117288000, y: 16},
%{x: 1570117296000, y: 16},
%{x: 1570117304000, y: 16}
]
iex> Enum.map(list, &Map.put(&1, :x, DateTime.from_unix!(&1.x, :millisecond)))
[
%{x: ~U[2019-10-03 15:41:20.000Z], y: 16},
%{x: ~U[2019-10-03 15:41:28.000Z], y: 16},
%{x: ~U[2019-10-03 15:41:36.000Z], y: 16},
%{x: ~U[2019-10-03 15:41:44.000Z], y: 16}
]
# or this classy one
iex> Enum.map(list, & %{&1 | x: DateTime.from_unix!(&1.x, :millisecond)})
3
Popular in Questions
How to bind a phoenix app to a specific ip address?
could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Other popular topics
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









