最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

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

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

Variable res would contain ['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 :

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

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

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

Variable res would contain ['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

Share Improve this question edited Feb 27, 2015 at 7:07 user1184100 asked Feb 27, 2015 at 6:58 user1184100user1184100 6,89430 gold badges84 silver badges122 bronze badges 5
  • just as null of undefined -> What do you mean by that? – thefourtheye Commented Feb 27, 2015 at 7:05
  • i've updated with a plnkr when you run it in console you'll see [undefined,undefined] – user1184100 Commented Feb 27, 2015 at 7:08
  • What do you want the expected output to be? – thefourtheye Commented Feb 27, 2015 at 7:09
  • null or just undefined instead of it being in an array – user1184100 Commented Feb 27, 2015 at 7:11
  • What if only one object doesn't have SomeRandomField and all others have that? – thefourtheye Commented Feb 27, 2015 at 7:11
Add a ment  | 

3 Answers 3

Reset to default 7

You can use filter and pluck:

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

You can use pact() to remove falsey values from the plucked array. You can use thru() to alter the output of the wrapper. In this case, we want null if all the plucked values are undefined.

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

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

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

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 .pluck and I don't believe that's possible.

_.pluck(collection, key)

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

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

What you can instead do is .pluck then use JavaScript's builtin (or lodash's) .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;
    }) 
}
发布评论

评论列表(0)

  1. 暂无评论