javascript _.pluck 的对象等价物是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18218902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the object equivalent of _.pluck
提问by Brad Dwyer
I've re-implemented my own version of what I need but I suspect this is already included in underscore since it's so simple and so closely related to many other functions. But I can't think of what it should be called.
我已经重新实现了我自己需要的版本,但我怀疑这已经包含在下划线中,因为它非常简单并且与许多其他功能密切相关。但我想不出应该叫什么。
Basically, what I want is a version of _.pluck that works with objects and returns an object instead of an array (with its associated keys).
基本上,我想要的是一个 _.pluck 版本,它可以处理对象并返回一个对象而不是一个数组(及其关联的键)。
So, for instance, if I had an object like this:
因此,例如,如果我有一个这样的对象:
elements: {
steam: {
temperature: 100,
color: 'orange',
state: 'gas'
},
water: {
temperature: 50,
color: 'blue',
state: 'liquid'
},
ice: {
temperature: 0,
color: 'white',
state: 'solid'
}
}
I'd want to call _.something(elements, 'temperature')
我想打电话 _.something(elements, 'temperature')
And have it return
让它回来
{
steam: 100,
water: 50,
ice: 0
}
Instead of _.pluck(elements, 'temperature')
which returns
而不是_.pluck(elements, 'temperature')
返回
[100, 50, 0]
What is this transformation called and is it already included in underscore? I've written a quick version myself with jQuery's each loop since I'm more familiar with jQuery than underscore (included below) but would prefer to use one from the library if possible.
这个转换叫做什么,它是否已经包含在下划线中?我自己用 jQuery 的 each 循环编写了一个快速版本,因为我对 jQuery 比下划线(包括在下面)更熟悉,但如果可能的话,我更愿意使用库中的一个。
$.objPluck = function(obj, key) {
var ret = {};
$.each(obj, function(k, value) {
ret[k] = value[key];
});
return ret;
}
回答by Mathletics
There's no method to do exactly this in underscore 1.4.4, but if you want to stay in underscore, you can do
在 underscore 1.4.4 中没有完全做到这一点的方法,但如果你想保持下划线,你可以这样做
_.object(_.keys(elements), _.pluck(elements, 'temperature'))
Demo at jsfiddlecourtesy of bfavaretto
As of underscore 1.8.3, this can be done succinctly with _.mapObject(elements, 'temperature');
.
从 underscore 1.8.3 开始,这可以通过_.mapObject(elements, 'temperature');
.
回答by taxilian
Worth adding in here a solution that uses ES6 arrow functions, etc:
值得在这里添加一个使用 ES6 箭头函数等的解决方案:
Object.keys(elements).map(f=>elements[f].temperature)
Note that this won't work on older javascript engines, but in node.js 0.12 with --harmony
it works great, or with node.js 4.x and later as well. I just tested this in Chrome 46 and it works fine.
请注意,这不适用于较旧的 javascript 引擎,但在 node.js 0.12 中--harmony
它工作得很好,或者在 node.js 4.x 和更高版本中也可以。我刚刚在 Chrome 46 中测试了这个,它工作正常。
It has the advantage of not needing any extra libraries such as underscore or lodash, but of course only works on new JS engines.
它的优点是不需要任何额外的库,例如下划线或 lodash,但当然只适用于新的 JS 引擎。
回答by pery mimon
on lodash (similar to underscore) , you can use _.mapValues(elements,'temperature')
.
it's return {steam: 100, water: 50, ice: 0}
在 lodash(类似于下划线)上,您可以使用_.mapValues(elements,'temperature')
.
回来了{steam: 100, water: 50, ice: 0}
lodash ref : _.mapValues
lodash 参考:_.mapValues
by the way you can use _.mapKeys
to build object hash in one line_.mapKeys(elements,'color') //{orange: Object, blue: Object, white: Object}
顺便说一下,您可以使用_.mapKeys
一行来构建对象哈希_.mapKeys(elements,'color') //{orange: Object, blue: Object, white: Object}
回答by daino3
You don't need 3rd party libraries for easy stuff like this
你不需要 3rd 方库来做这样简单的事情
// with a plain old for loop
const temps = {}
for (let key in data["elements"]) {
dikt[key] = temps["elements"][key]["temperature"]
}
return temps
// with a reduce
Object.keys(data['elements']).reduce((acc, key) => acc[key] = data['elements'][key]['temperature'] , temps)
回答by Hjulle
In newer versions there is actually a built in function which does this.
在较新的版本中,实际上有一个内置函数可以做到这一点。
If you give a string, instead of a function, to mapObject
, it will interpret that as a property name and behave as a "pluckObject
". Most functions are surprisingly liberal in what they accept.
如果你给一个字符串,而不是一个函数, to mapObject
,它会将它解释为一个属性名称并表现为一个“ pluckObject
”。大多数函数在接受的内容上出奇地自由。
> _.mapObject(elements,'temperature')
Object {steam: 100, water: 50, ice: 0}
If you want an explicit function for the purpose, you can look at how _.pluck
is implementedand write your own analogous version:
如果你想为宗旨明确的功能,你可以看看是如何_.pluck
被实现的,写自己的类似的版本:
_.mixin({
pluckObject: function(obj, key) {
return _.mapObject(obj, _.property(key));
}
});
This is a bit redundant, but it makes your intentions clearer.
这有点多余,但它使您的意图更加清晰。
回答by Zorayr
You can use a JavaScript for-in loopto iterate through the keys of the object:
您可以使用JavaScript for-in 循环遍历对象的键:
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 is a simplified example, but you should be able to easily modify to encapsulate it in a more generic function. Also, you don't really need to use JQuery here - native JavaScript implementation will be faster.
上面是一个简化的示例,但您应该能够轻松修改以将其封装在更通用的函数中。此外,您真的不需要在这里使用 JQuery - 原生 JavaScript 实现会更快。
The obj.hasOwnProperty(name)
check is recommended by JSLint.
该obj.hasOwnProperty(name)
检查由JSLint推荐。