Clojure。更新双嵌套值

问题描述 投票:0回答:2

我是Clojure的新手,虽然很新,但也很有趣。所以我有这个数据:

 {:test {:title "Some Title"}, :questions [
   {:id 1, :full-question {:question "Foo question", :id 1, :answers [{:id 7, :question_id 1, :answer "Foobar answer"}, {:id 8, :question_id 1, :answer "Foobar answer two"}]}},
   {:id 5, :full-question {:question "Foo question", :id 5, :answers [{:id 12, :question_id 5, :answer "Foobar answer"}]}},
   {:id 9, :full-question {:question "Foo question", :id 9, :answers [{:id 14, :question_id 9, :answer "Foobar answer"}, {:id 20, :question_id 9, :answer "Foobar answer two"}]}}
 ]}

一种“经典” 测试->问题->答案一种信息。我有这个新信息:

 (def new-answer {:id 33, :answer "Another foobar answer", :question-id 9 })

我需要更新第一个结构,将添加到:questions向量中的:id数字9的“答案”中。

我尝试使用update-in函数,但是我不知道在两个向量内的映射中告诉对应的:id是什么。我的意思是,我不知道如何在要更改的位置构建“路径”。

clojure clojurescript
2个回答
0
投票

您有进行更新的正确想法。您可以先在问题向量中计算索引,然后创建路径:questions,“ question-index”,:full-question,:answers。然后,您可以在新答案中使用conj:


0
投票

而且,还有一个很好的库用于这种结构编辑,称为specter

© www.soinside.com 2019 - 2024. All rights reserved.