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
Hi @shahryarjb, what have you tried so far?
1
shahryarjb
I think this should be this

new_blocks
|> Enum.with_index
|> Enum.map(fn {x, index} ->
Map.merge(x, %{index: index})
end)
|> IO.inspect()
1
chasers
Like this?
defmodule MyApp.Indexer do
def put_index(list, index \\ 0)
def put_index([], _) do
[]
end
def put_index([head | tail], index) do
[Map.put(head, :index, index) | put_index(tail, index + 1)]
end
end
Check out the Enum.with_index source: https://github.com/elixir-lang/elixir/blob/v1.11.2/lib/elixir/lib/enum.ex#L3152
1
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I would like to know what is the best IDE for elixir development?
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New







