What is the equivalent of _.pluck

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; } 
+6
source share
6 answers

There is no way to do just that in underscore 1.4.4 , but if you want to stay in underscore, you can do

 _.object(_.keys(elements), _.pluck(elements, 'temperature')) 

Jsfiddle trial version provided by bfavaretto


As for the underscore 1,8,3 , this can be done concisely with _.mapObject(elements, 'temperature'); .

Updated demo

+8
source

It’s worth adding here a solution that uses ES6 arrow functions, etc.:

 Object.keys(elements).map(f=>elements[f].temperature) 

Please note that this will not work on older javascript machines, but in node.js 0.12 with --harmony it works fine either with node.js 4.x and later. I just tested this on Chrome 46 and it works great.

This has the advantage of not needing additional libraries such as underscores or lodash, but, of course, only works on new JS machines.

+5
source

on lodash (similar to underscore), you can use _.mapValues(elements,'temperature') .
it returns {steam: 100, water: 50, ice: 0}

lodash ref: _. mapValues

since you can use _.mapKeys to create an object hash in one line
_.mapKeys(elements,'color') //{orange: Object, blue: Object, white: Object}

+3
source

You can use the JavaScript for-in loop to repeat object keys:

 var list = []; // Iterate through each object key, and pick wanted the values. for(var name in obj) { if (obj.hasOwnProperty(name) && name === "temperature") { list.push(obj[name]); } } 

The above example is simplified, but you can easily modify it to encapsulate it into a more general function. In addition, you do not need to use jQuery here - the JavaScript implementation will be faster.

Checking obj.hasOwnProperty(name) recommended by JSLint .

0
source

Newer versions actually have a built-in function that does this.

If you pass a string, not a function, to mapObject , it will interpret it as the name of the property and behave like " pluckObject ". Most features are surprisingly liberal in what they accept.

 > _.mapObject(elements,'temperature') Object {steam: 100, water: 50, ice: 0} 

If you need an explicit function for this purpose, you can see how _.pluck implemented and write your own similar version:

 _.mixin({ pluckObject: function(obj, key) { return _.mapObject(obj, _.property(key)); } }); 

This is a little redundant, but it makes your intentions clearer.

0
source

For whom it may be of interest - At the moment, it is best to use the built-in js instead of external libraries.

Object.values(elements).map(elem => elem.temperature) .

0
source

Source: https://habr.com/ru/post/951671/


All Articles