[英]How to get first element map from an array using clojure
I have an array of result ({:a one})
. 我有一个结果数组
({:a one})
。 I want a result which returns one when I select or give :a
. 我想要一个当我选择或给出
:a
时返回一个结果的结果。
I used vals,get-in
functions but since the map is inside a list they are not working. 我使用了
vals,get-in
函数,但是由于映射位于列表内,因此它们无法正常工作。 Can you please help me how do I do that. 您能帮我怎么做。
I need to return value for a given key. 我需要返回给定键的值。
Also, 也,
What if the data is of the form 如果数据采用以下格式怎么办
({:a “Favorite fruit", :measurements [{:a “fav game?", :b "x",
:name “foot ball", :max 7.0, :min 0.0, :id “12345"}], :name “Frequency”})
and I want to select :a "fav game?"
我想选择
:a "fav game?"
:name "foot ball"
and :min 0.0
? :name "foot ball"
和:min 0.0
?
Thank you 谢谢
Try this: 尝试这个:
clj.core=> (def data [ {:a 1} {:b 2} ] )
#'clj.core/data
clj.core=> (:a (first data))
1
clj.core=> (mapv :a data)
[1 nil]
List comprehension using for with any quantity of elements in the list: 使用列表中理解为与列表中的元素中的任何数量:
(for [x data :let [m (:measurements x)]] (map #(let [{:keys [a name min]} %] [a name min]) m))
Details: 细节:
for [x data
for every element in data for [x data
中的每个元素 :let [m (:measurements x)]]
set m
as a symbol for :measurements
:let [m (:measurements x)]]
将m
设置为:measurements
的符号 (map #(let [{:keys [a name min]} %] [a name min]) m)
for every m
get the keys a name min
and output a vector [a name min]
. (map #(let [{:keys [a name min]} %] [a name min]) m)
每m
获得一个键a name min
并输出向量[a name min]
。 Output: 输出:
((["fav name?" "foot ball" 0.0]))
Directly using apply : 直接使用apply :
(let [{:keys [a name min]} (apply (comp first :measurements) data)] [a name min])
Details: 细节:
(apply (comp first :measurements) data)
apply comp
osition, first get :measurements
then the first
elem. (apply (comp first :measurements) data)
应用comp
,首先获取:measurements
然后是first
elem。 let [{:keys [a name min]}
get the keys a name min
from the map and output a vector [a name min]
. let [{:keys [a name min]}
从映射中获取keys a name min
,并输出矢量[a name min]
。 Output: 输出:
["fav name?" "foot ball" 0.0]
If you have a vector of results (usually more idiomatic than a list), you can use get in, but with numbers as keys. 如果您有结果向量(通常比列表更惯用),则可以使用get in,但将数字用作键。 eg
例如
(get-in [{:a "one"}] [0 :a]) ;=> "one"
To add to @Marcs answer (and for some extra Clojure help) there are a few ways to pull values out using destructuring too. 要添加到@Marcs答案(以及一些其他Clojure帮助)中,也有几种使用解构来提取值的方法。
Starting with the example you gave 从您给出的示例开始
(def foobar
{:a "Favorite fruit", :measurements [{:a "fav game?", :b "x",
:name "foot ball", :max 7.0, :min 0.0, :id "12345"}], :name "Frequency"})
We can do associative destructuring since Clojure maps are associative data structures 由于Clojure映射是关联数据结构,因此我们可以进行关联解构
;; associative destructuring
(let [{:keys [a]} foobar]
a)
;; also associative destructuring in the function params this time
((fn [{:keys [a]}]
a)
foobar)
To a get single value out of a map wrapped in an array we can use a combination of sequential and associative destructuring 为了从包装在数组中的映射中获取单个值,我们可以结合使用顺序分解和关联分解
(let [[{:keys [a]}] '({:a 'one})]
a)
For the last part you asked about with getting :a :name :min
we can use another destructuring combo 对于最后一个关于获取
:a :name :min
我们可以使用另一个解构组合
(let [{a :a [{min :min}] :measurements name :name} foobar]
[a name min])
That clojure.org article I linked to is pretty awesome for explaining more details! 我链接的那篇clojure.org文章对于解释更多细节非常棒!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.