如何更新列表的一个元素

Bou*_*TAC 2 dictionary list updates elm

我有一个update功能,采取postID和标题来更新我的帖子.

我想循环搜索帖子以查找帖子并更新其值.我试过用List.map但我不知道放在哪里.这是我想要的伪代码:

update action model =
  case action of
    UpdateTitle postID title ->
      //something like this:
      for post in model.posts :
        if post.id == postID then
          { post | title = title }

      ( model.posts , Effects.none )
Run Code Online (Sandbox Code Playgroud)

Cha*_*ert 6

您可以使用List.map传递仅使用匹配ID更新帖子的映射函数.

update action model =
  case action of
    UpdateTitle postID title ->
      ( List.map (setTitleAtID title postID) model.posts , Effects.none )

setTitleAtID title postID post =
  if post.id == postID then
    { post | title = title }
  else
    post
Run Code Online (Sandbox Code Playgroud)