交换在键值查找中嵌套的特定项,在键中的位置

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

我们有一个带有此示例数据的光标或原子图:

    #<Cursor: [:customer] {:name Diego Peña, 

    :addresses [{:id 23, :province Madrid, :country 1, :descripcion aaeeeeeeee iii oooo4444, :locality Gali gali, :country_name SPAIN, :direccion Street Cierva, :id 3, :postalcode 30203, :principal true, :customer 17} 

{:id 35, :province Madrid, :country nil, :descripcion yyy lalala3, :locality Lalala, :direccion calle Maria 3 , :postalcode 333, :principal false, :customer 17} 

{:id 6, :province Madrid, :country 2, :descripcion otra direccioncita444, :locality Leleele, :country_name SPAIN, :direccion Direccion calle Ooo, :postalcode 1236, :main false, :customer 17} 

{:id 27, :province Madrid, :country 1, :descripcion grandisima, :locality Alcantarilla, :country_name SPAIN, :direccion C/ 3 Mayo, :postalcode 3001, :main false, :customer 17}

]}>

我需要通过ID更改搜索地址的值。我设法通过id的值找到地址:

(defn get-address [pk]
   (->> @db/customer :addresses (filter #(= (int pk) (int (:id %)))) first)
)

我可以通过以下方式更改所有地址::ok #(swap! db/customer assoc-in [:addresses] %)})。我需要从API响应中更改特定地址的数据。

我快要拿到它,但是采用这种方法,我错过了商品的位置或索引:#(swap! db/client assoc-in [:addresses ¿position or index in map?] %)我们有商品地址的ID。

也许这种方法是错误的,更好的方法?

clojure clojurescript
2个回答
0
投票

assocassoc-inupdateupdate-in函数也可用于矢量。在Clojure中,向量是关联数据结构,其中键是数字索引(O..n),值是位置n处的项。


0
投票

似乎您正在从某种数据库中提取数据。如果是这样,您应该让数据库搜索有问题的ID。然后,您可以阅读或更新该记录。

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