Updating nested list records

My use-case
I want to end up with several forms on one page to update each nested child record, so my first attempt was to List.map over the children and output input elements, but how might I pass the input element to which child item my input box relates to? If I could pass my map function an index it’d all be fine :slight_smile:

Wondering what the ‘best practice’ way of updating a nested list of records might be — a typical parent/child example JSON might be:

{
  "parents": [
    {
      "id": "0001",
      "children": [
        {
          "desc": "Description", "attr": "val", ... etc
        },
        {
          "desc": "Description 2", "attr": "val 2", ... etc
        }
      ]
    },
  ]
}

I can see how you’d do this by giving each child record an ID and then doing an update based a filtered list (and similarly if it was more of a flat, relational database style where each child item had a reference to the parent id and their own id attribute).

Should I be looking to map that JSON to some different structure, perhaps giving each child item an index attribute to then use as if the source JSON gave each record it’s own ID, or some other way?

Like this?

http://package.elm-lang.org/packages/elm-lang/core/5.1.1/List#indexedMap

That was simple :slight_smile: thanks.

module Main exposing (main)

import Html exposing (Html)


main : Html msg
main =
    Html.div [] (List.indexedMap m items)


items = [
    { contents= "bob" }, { contents= "foo" }]

m idx item =
    Html.div [] [Html.text (toString idx), Html.text item.contents]

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.