shahryarjb
How to edit and sort index of nested map in a list
Hello, I have a list like this:
[
%{
index: 1,
params: %{
id: "test1",
title: "test of one",
width: "col-sm-12"
},
type: "Heading"
},
%{
index: 3,
params: %{
id: "test3",
title: "test of one",
width: "col-sm-12"
},
type: "Heading"
},
%{
index: 5,
params: %{
id: "test5",
title: "test of one",
width: "col-sm-12"
},
type: "Heading"
},
]
and I want to change all index number and sort them but with new index which starts 0 like this
[
%{
index: 0,
params: %{
id: "test1",
title: "test of one",
width: "col-sm-12"
},
type: "Heading"
},
%{
index: 1,
params: %{
id: "test4",
title: "test of one",
width: "col-sm-12"
},
type: "Heading"
},
%{
index: 2,
params: %{
id: "test5",
title: "test of one",
width: "col-sm-12"
},
type: "Heading"
},
]
I need this because maybe a map is deleted in a list and the map deleted index will be null and I want to re-index all the map after every deleting
Thanks
Most Liked
chasers
If you wanted to use with_index you could do this but you’d be going through those more than you need to:
Enum.with_index(list) |> Enum.map(fn {x, index} -> Map.put(x, :index, index) end)
2
eksperimental
Here’s a one-liner.
Enum.reduce(list, {0, []}, fn element, {index, list_acc} -> {index + 1, [%{element | index: index} | list_acc]} end)
|> elem(1)
|> Enum.reverse()
2
benwilson512
Author of Craft GraphQL APIs in Elixir with Absinthe
Popular in Questions
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
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
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
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
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
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









