简体   繁体   English

Clojure,更新地图中的嵌套内容

[英]Clojure, update nested content in map

I am confused about the required syntax for updating a value in a map, where said value is a vector of maps. 我对更新地图中的值所需的语法感到困惑,其中所述值是地图的向量。

Given a map: 给定一张地图:

   {:data-extracts [
                       {:name "some name"
                        :data "some data"
                        }]}

How can I update the value of :data, I know you can use assoc or conj to modify maps (well return new maps) but I'm unsure how this works when nested elements are present. 我如何更新:data的值,我知道您可以使用assocconj来修改地图(很好地返回新地图),但是我不确定当存在嵌套元素时这是如何工作的。

desired result: 预期结果:

   {:data-extracts [
                       {:name "some name"
                        :data "new data"
                        }]}

Is there a way to do something like the following? 有没有办法做下面的事情?

(update :data-extracts :data "new data")

How is this achieveable? 这是如何实现的?

I tried the following: 我尝试了以下方法:

(assoc opts :data-extracts [:name "Secret Escapes"
                            :data "new data"]))

But that doesn't work as I expected. 但这不符合我的预期。

When using nested structures, you'll want to use *-in -functions (in this case assoc-in ) and specify one key for each nesting level. 使用嵌套结构时,您需要使用*-in -functions(在这种情况下为assoc-in ),并为每个嵌套级别指定一个键。 In case of vectors, it's just index, in case of maps it's obvious: 对于矢量,它只是索引,对于地图,这是显而易见的:

(assoc-in [:data-extracts 0 :data] "new data") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM