简体   繁体   English

在嵌套哈希图中创建多个相同深度值的集合

[英]Creating a collection of multiple same-depth values in a nested hashmap

Here is a hypothetical hashmap named args: 这是一个名为args的假设哈希图:

{:body {:milestones [{:status 1 :otherValues x} 
                     {:status 2 :otherValues z} 
                     {:status 1 :otherValues y]}}

My goal is to have a collection of the values for each :status key. 我的目标是为每个:status键收集值。 They are all at the same depth being the child of :milestones. 它们都是:里程碑的子元素,深度相同。

I'm getting close. 我越来越近了 I know how to retrieve the value of the first status by doing this: 我知道如何通过执行以下操作来检索第一个状态的值:

(let [{[{:keys [status]} x] :milestones} :body} args]
  (println status))

The very far end goal is to find out which maps contain a :status with a value of 1 and create a new collection with each individual map. 最终目标是找出哪些地图包含值1的:status,并为每个单独的地图创建一个新集合。

The literal application of this is connecting to TeamworkPM and syncing up milestones with a status of "late" or "incomplete" with Google Calenders. 字面上的应用是连接到TeamworkPM并与Google日历同步状态为“迟到”或“未完成”的里程碑。

Desired output would be {1, 2, 1} in this scenario. 在这种情况下,所需的输出将为{1、2、1}。 The end goal is to have 最终目标是拥有

 {{:status 1 :otherValues x} 
  {:status 1 :otherValues Y}}

Although I couldn't find out how to destructure the vector of map into a variable directly, instead you can first get the child of :milestones , and then use basic map or filter . 尽管我找不到如何将map的向量直接分解为变量的方法,但是您可以首先获取:milestones的子级,然后使用basic mapfilter

Note that you can get the value of map by applying it as function. 请注意,您可以通过将map用作函数来获取其值。 (eg if m is {:key1 "val1"} , (m :key1) would be "val1" ) (例如,如果m{:key1 "val1"} ,则(m :key1)"val1"

(def args {:body {:milestones [{:status 1 :otherValues 'x}
                               {:status 2 :otherValues 'z}
                               {:status 1 :otherValues 'y}]}})

(let [{{x :milestones} :body} args,
        y (map #(% :status) x),
        z (filter #(= (% :status) 1) x)
      ]
      (println x) ; [{:status 1, :otherValues x} {:status 2, :otherValues z} {:status 1, :otherValues y}]
      (println y) ; (1 2 1)
      (println z) ; ({:status 1, :otherValues x} {:status 1, :otherValues y})
  )

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

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