shahryarjb

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

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)
eksperimental

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()
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @shahryarjb, what have you tried so far?

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
vertexbuffer
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
hariharasudhan94
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
Harrisonl
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
qwerescape
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
johnnyicon
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
ycv005
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
minhajuddin
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
lanycrost
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 Top

axelson
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...
239 48475 226
New
hariharasudhan94
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
shahryarjb
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
boundedvariable
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
openscript
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
dogweather
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

We're in Beta

About us Mission Statement