I updated my version of what I need, but I suspect that this is already included in the underline, as it is so simple and so closely related to many other functions. But I canβt figure out what to call it.
Basically, I want a version of _.pluck that works with objects and returns an object instead of an array (with its associated keys).
So, for example, if I had such an object:
elements: { steam: { temperature: 100, color: 'orange', state: 'gas' }, water: { temperature: 50, color: 'blue', state: 'liquid' }, ice: { temperature: 0, color: 'white', state: 'solid' } }
I would like to call _.something(elements, 'temperature')
And bring him back
{ steam: 100, water: 50, ice: 0 }
Instead of _.pluck(elements, 'temperature') , which returns
[100, 50, 0]
What is called this conversion and is it already included in the underline? I wrote my own version with jQuery every cycle, since I am more familiar with jQuery than underscore (see below), but, if possible, prefer to use it from the library.
$.objPluck = function(obj, key) { var ret = {}; $.each(obj, function(k, value) { ret[k] = value[key]; }); return ret; }