Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在clojure中同时使用assoc和dissoc转换映射_Clojure_Clojurescript - Fatal编程技术网

在clojure中同时使用assoc和dissoc转换映射

在clojure中同时使用assoc和dissoc转换映射,clojure,clojurescript,Clojure,Clojurescript,我有一张地图,如下所示。我从一个数据库里得到信息。现在我想在这里转换数据结构: (def my-map [{:db/id #object[Object 56536887900242005], :height 630, :distance 1474.1, :coordinates [-26.65622109697031 30.48401767312403], :l

我有一张地图,如下所示。我从一个数据库里得到信息。现在我想在这里转换数据结构:

(def my-map [{:db/id #object[Object 56536887900242005],
                 :height 630,
                 :distance 1474.1,
                 :coordinates [-26.65622109697031 30.48401767312403],
                 :location #:location{:id 1}}
                {:db/id #object[Object 56536887900242006],
                 :height 22075,
                 :distance 1503.2,
                 :coordinates [-26.65622109697031 30.48401767312403],
                 :location #:location{:id 2}}
                {:db/id #object[Object 56536887900242007],
                 :height 24248,
                 :distance 1695.6,
                 :coordinates [-26.662030943549 30.25648873549992],
                 :location #:location{:id 3}})
像这样

{1 {:height 630, :distance 1474.1,:coordinates [-26.65622109697031 30.48401767312403]}
 2 {:height 22075, :distance 1503.2,:coordinates [-26.65622109697031 30.48401767312403]}
 3 {:height 24248, :distance 1695.6,:coordinates [-26.65622109697031 30.48401767312403]}}
我想从:location{:ID1}中提取1,然后与之关联

下面我有返回上述内容的代码,但我不知道如何将其关联到:id中,而且我也不知道如何获取id,因为数据有一个


你可以这样写:

(into {} 
  (map
    #(hash-map 
       (get-in % [:location :location/id]) 
       (dissoc % :db/id :location))
    my-map))
无论如何,我在每一个项目中都使用Pimming.core,如果您也这样做,那么这个解决方案可能会吸引您

(grouped-map
    (fn-> :location :location/id)
    (fn-> (dissoc :location :db/id))
    my-map)
有时,使用for有助于使数据结构更加清晰:

(->> (for [record my-map]
       [(-> record :location :location/id)
        (dissoc record :db/id :location)])
     (into {}))

以下函数将执行此操作:

(defn transform [coll]
  (->> coll
       (map #(hash-map
               (-> % :location :location/id)
               (dissoc % :db/id :location)))
       (into {})))

类似这样的操作可以实现以下目的:将assoc%1->%2:location:location/id dissoc%2:db/id:location{}my映射或类似的操作:将comp:location/id:location dissoc%还原为{}map juxt comp:location/id:location dissoc%:location:db/id my-map@leetwinski如果你想回答,你应该张贴一个答案。
(->> (for [record my-map]
       [(-> record :location :location/id)
        (dissoc record :db/id :location)])
     (into {}))
(defn transform [coll]
  (->> coll
       (map #(hash-map
               (-> % :location :location/id)
               (dissoc % :db/id :location)))
       (into {})))