javascript _.pluck 在找不到对象时给出未定义值的数组

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

_.pluck gives an array of undefined values when it does not find the object

javascriptlodash

提问by user1184100

I'm using _.pluck() from lodash to get the value of the keys from an array.

我正在使用 lodash 的 _.pluck() 从数组中获取键的值。

var employees = [
  {
    Name : "abc"  
  },
  {
    Name : "xyz"
  }
]

var res = _.pluck(employees, 'Name'); 

Variable res would contain ['abc,'xyz']

变量 res 将包含 ['abc,'xyz']

When I do a search for some other field field

当我搜索其他字段时

var res = _.pluck(employees, 'SomeRandomField');   

Result - [undefined, undefined]

结果 - [未定义,未定义]

How can I get the above result just as null of undefined instead of an array of undefined values

我怎样才能得到上面的结果就像未定义的空值而不是未定义值的数组

Plnkr : http://plnkr.co/edit/qtmm6xgdReCuJP5fm1P2?p=preview

Plnkr:http://plnkr.co/edit/qtmm6xgdReCuJP5fm1P2?p=preview

采纳答案by royhowie

I looks like you're actually looking for the .somefunction:

我看起来您实际上正在寻找该.some功能:

var res = _.pluck(employees, "Name");
res = res.some(function (d) { return d }) ? // are any of the elements truth-y?
    // if so, map the  false-y items to null
    res.map(function (item) { return item || null; }) :
    // otherwise (no truth-y items) make res `null`
    null;


I took a look at the lodash documentation for .pluckand I don't believe that's possible.

我查看了 lodash 文档.pluck,我认为这是不可能的。

_.pluck(collection, key)

Arguments collection (Array|Object|string): The collection to iterate over.

key (string): The key of the property to pluck.

_.pluck(collection, key)

参数集合(Array|Object|string):要迭代的集合。

key(字符串):要提取的属性的键。

What you can instead do is .pluckthen use JavaScript's builtin (or lodash's) .map:

你可以做的是.pluck使用 JavaScript 的内置(或 lodash 的).map

var res = _.pluck(employees, 'Name').map(function (d) {
    return d ? d : null;
});

Which is rather inefficient. You might as well write your own function that only iterates over the array once:

这是相当低效的。您不妨编写自己的函数,该函数仅对数组进行一次迭代:

_.nullPluck = function (arr, key) {
    return arr.map(function (d) {
        return d && d[key] ? d[key] : null;
    }) 
}

回答by krynio

You can use filterand pluck:

您可以使用filterpluck

var res = _.filter(_.pluck(employees, 'Name'), function(item) {
    return item;
});

回答by Adam Boduch

You can use compact()to remove falseyvalues from the plucked array. You can use thru()to alter the output of the wrapper. In this case, we want nullif all the plucked values are undefined.

您可以使用compact()从提取的数组中删除falsey值。您可以使用thru()来更改包装器的输出。在这种情况下,我们希望null所有提取的值都为undefined

var collection = [ {}, {}, {} ];

_(collection)
    .pluck('foo')
    .compact()
    .thru(function(coll) { return _.isEmpty(coll) ? null : coll; })
    .value();
// → null