Xaritalar M va M qiymatlari A ning elementlarida yangi tugmachalar bilan eşleştirildiği M xarita xaritalarini o'zgartiring

Quyidagi kabi xaritani:

{
 "keystring1" "valuestring1"
 "keystring2" "valuestring2"
 ...
}

Buni qanday qilib xaritalar qatoriga aylantira olaman:

[
 {
  :newKey1 "keystring1"
  :newKey2 "valuestring1"
 }
 {
  :newKey1 "keystring2"
  :newKey2 "valuestring2"
 }
 ...
]

Tadqiqotlarimdan kelib chiqqan holda reduce-kv va assoc funktsiyalari bu erda foydali bo'lishi kerak deb hisoblayman, lekin hali ishchi versiyani yaratolmadim.

Men hozirgacha qo'lga kiritgan yutuqlar:

 (reduce-kv (fn [m k v]
   (merge m (assoc {} :newkey1 k) (assoc {} :newkey2 v)))
   []
   {"keystring1" "valuestring1", "keystring2" "valuestring2"})

Quyidagi natijalar:

[{:newkey1 "keystring1"} {:newkey2 "valuestring1"} {:newkey1 "keystring2"} {:newkey2 "valuestring2"}]
1

7 javoblar

Bu kabi narsalar uchun uchun dan foydalanishni yaxshi ko'raman.

(def m {"keystring1" "valuestring1"
        "keystring2" "valuestring2"})

> (for [[k v] m]
    {:newkey1 k
     :newkey2 v})

({:newkey1 "keystring1", :newkey2 "valuestring1"}
 {:newkey1 "keystring2", :newkey2 "valuestring2"})

Agar kerak bo'lsa, uni vektor qilib majburlashingiz mumkin:

> (vec (for [[k v] m]
         {:newkey1 k
          :newkey2 v}))

[{:newkey1 "keystring1", :newkey2 "valuestring1"}
 {:newkey1 "keystring2", :newkey2 "valuestring2"}]
3
qo'shib qo'ydi

Bu kabi narsalar uchun uchun dan foydalanishni yaxshi ko'raman.

(def m {"keystring1" "valuestring1"
        "keystring2" "valuestring2"})

> (for [[k v] m]
    {:newkey1 k
     :newkey2 v})

({:newkey1 "keystring1", :newkey2 "valuestring1"}
 {:newkey1 "keystring2", :newkey2 "valuestring2"})

Agar kerak bo'lsa, uni vektor qilib majburlashingiz mumkin:

> (vec (for [[k v] m]
         {:newkey1 k
          :newkey2 v}))

[{:newkey1 "keystring1", :newkey2 "valuestring1"}
 {:newkey1 "keystring2", :newkey2 "valuestring2"}]
3
qo'shib qo'ydi

Bu erda reduce-kv dan foydalangan holda yechim mavjud:

(def my-map
  {"keystring1" "valuestring1"
   "keystring2" "valuestring2"})

(reduce-kv
  (fn [acc k v] (conj acc {:newKey1 k :newKey2 v}))
  []
  my-map)

Yoki zipmap dan foydalaning:

(mapv #(zipmap [:newKey1 :newKey2] %) my-map)

Har ikkisi ham shuni ishlab chiqaradi:

=> [{:newKey1 "keystring1", :newKey2 "valuestring1"}
    {:newKey1 "keystring2", :newKey2 "valuestring2"}]
2
qo'shib qo'ydi
Tashakkur, mapv va zipmap yordamida echilgan narsa, albatta, toza!
qo'shib qo'ydi muallif Jdv, manba
FWIW Men uchun yondashuvi bilan borishni xohlayman. Men bularni alternativ sifatida qo'shdim.
qo'shib qo'ydi muallif Taylor Wood, manba

Bu erda reduce-kv dan foydalangan holda yechim mavjud:

(def my-map
  {"keystring1" "valuestring1"
   "keystring2" "valuestring2"})

(reduce-kv
  (fn [acc k v] (conj acc {:newKey1 k :newKey2 v}))
  []
  my-map)

Yoki zipmap dan foydalaning:

(mapv #(zipmap [:newKey1 :newKey2] %) my-map)

Har ikkisi ham shuni ishlab chiqaradi:

=> [{:newKey1 "keystring1", :newKey2 "valuestring1"}
    {:newKey1 "keystring2", :newKey2 "valuestring2"}]
2
qo'shib qo'ydi
Tashakkur, mapv va zipmap yordamida echilgan narsa, albatta, toza!
qo'shib qo'ydi muallif Jdv, manba
FWIW Men uchun yondashuvi bilan borishni xohlayman. Men bularni alternativ sifatida qo'shdim.
qo'shib qo'ydi muallif Taylor Wood, manba

Bu erda reduce-kv dan foydalangan holda yechim mavjud:

(def my-map
  {"keystring1" "valuestring1"
   "keystring2" "valuestring2"})

(reduce-kv
  (fn [acc k v] (conj acc {:newKey1 k :newKey2 v}))
  []
  my-map)

Yoki zipmap dan foydalaning:

(mapv #(zipmap [:newKey1 :newKey2] %) my-map)

Har ikkisi ham shuni ishlab chiqaradi:

=> [{:newKey1 "keystring1", :newKey2 "valuestring1"}
    {:newKey1 "keystring2", :newKey2 "valuestring2"}]
2
qo'shib qo'ydi
Tashakkur, mapv va zipmap yordamida echilgan narsa, albatta, toza!
qo'shib qo'ydi muallif Jdv, manba
FWIW Men uchun yondashuvi bilan borishni xohlayman. Men bularni alternativ sifatida qo'shdim.
qo'shib qo'ydi muallif Taylor Wood, manba

va yana bir, to'liqligi uchun:

user> (map (partial zipmap [:new-key1 :new-key2]) data)

;;=> ({:new-key1 "keystring1", :new-key2 "valuestring1"} 
;;    {:new-key1 "keystring2", :new-key2 "valuestring2"})

yoki

user> (map zipmap (repeat [:new-key1 :new-key2]) data)

;;=> ({:new-key1 "keystring1", :new-key2 "valuestring1"} 
;;    {:new-key1 "keystring2", :new-key2 "valuestring2"})
1
qo'shib qo'ydi

va yana bir, to'liqligi uchun:

user> (map (partial zipmap [:new-key1 :new-key2]) data)

;;=> ({:new-key1 "keystring1", :new-key2 "valuestring1"} 
;;    {:new-key1 "keystring2", :new-key2 "valuestring2"})

yoki

user> (map zipmap (repeat [:new-key1 :new-key2]) data)

;;=> ({:new-key1 "keystring1", :new-key2 "valuestring1"} 
;;    {:new-key1 "keystring2", :new-key2 "valuestring2"})
1
qo'shib qo'ydi