javascript 使用 lo-dash/underscore 从 _.pluck 获取对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18632841/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:40:28  来源:igfitidea点击:

Getting an object from _.pluck with lo-dash/underscore

javascriptunderscore.jslodash

提问by JJJollyjim

I have an object structured like this:

我有一个这样结构的对象:

var my_object = {
  first_item: {important_number: 4},
  second_item: {important_number: 6},
}

However, I would like an object structured like this:

但是,我想要一个结构如下的对象:

{
  first_item: 4,
  second_item: 6,
}

I would have expected to be able to get this result with _.pluck:

我本来希望能够得到这个结果_.pluck

_.pluck(my_object, "important_number")

But this gives me:

但这给了我:

[0: 4, 1: 6]

Good, but I need the actual names of the objects. I fiddled around and ended up with this:

很好,但我需要对象的实际名称。我摆弄了一下,最后得到了这个:

_.reduce(my_object, function(memo, val, key) {
  memo[key] = val.content;
  return memo;
}, {});

Which has the desired effect, but isn't as simple as I would?like. Is there a better solution in underscore/lodash, or is this the best it will get?

哪个具有预期的效果,但并不像我想要的那么简单?下划线/lodash 是否有更好的解决方案,或者这是最好的解决方案?

回答by John-David Dalton

In Lo-Dash you could also use _.transform, a more powerful alternative to _.reduce:

在 Lo-Dash 中,您还可以使用_.transform,这是_.reduce的更强大的替代

_.transform(my_object, function(memo, val, key) {
  memo[key] = val.important_number;
});

回答by TWiStErRob

Actually what you're describing - that is "pluckfor object values" - can be written as:

实际上你所描述的 - 即“pluck对象值” - 可以写成:

_.mapValues(my_object, "important_number")

See documentation of _.mapValues.

请参阅 的文档_.mapValues

_.createCallback- which does the string to property magic - is used all over the place in Lo-Dash: search for "_.pluck" style callback.

_.createCallback- 将字符串转换为属性魔法 - 在 Lo-Dash 中到处使用:搜索"_.pluck" style callback.

回答by Gilad Peleg

I know it's fun to use Lo-Dash/Underscore, but if you have to use 2 functions instead of just using a regular loop, you're better off using 'old_school' coding style:

我知道使用 Lo-Dash/Underscore 很有趣,但是如果您必须使用 2 个函数而不是仅使用常规循环,则最好使用“old_school”编码风格:

var data = {
  first_item: {important_number: 4},
  second_item: {important_number: 6},
};

var obj = {};

for (var i in data) {
  if (data.hasOwnProperty(i)) obj[i] = data[i].important_number;  
}

Just to prove my point, I updated your JsPerf to include the regular for loop, and watch the magic: http://jsperf.com/getting-an-object-from-pluck/2

为了证明我的观点,我更新了您的 JsPerf 以包含常规 for 循环,并观看魔术:http: //jsperf.com/getting-an-object-from-pluck/2

Regular For Loopis at least 2xfaster than any other method you guys posted here. And that's mainly because it only traverses the object once.

常规 For 循环至少比你们在此处发布的任何其他方法快2倍。这主要是因为它只遍历对象一次。