Lodash _.pluck does this
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
_.pluck(users, 'user');
// → ['barney', 'fred']
Good thing about it is it can also go deep like this:
var users = [
{ 'user': {name: 'barney'}, 'age': 36 },
{ 'user': {name: 'fred'}, 'age': 40 }
];
_.pluck(users, 'user.name');
// ["barney", "fred"]
Is there equivalent in Clojure core of this? I mean, I can easily create one line somewhat like this
(defn pluck
[collection path]
(map #(get-in % path) collection))
And use it like this:
(def my-coll [{:a {:z 1}} {:a {:z 2}}])
(pluck my-coll [:a :z])
=> (1 2)
I was just wondering if there's such thing already included in Clojure and I overlooked it.
Here are simple tricks:
Works because keyword behaves like a function as well.