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

javascript - Lo-Dash - help me understand why _.pick doesn't work the way I expect - Stack Overflow

programmeradmin0浏览0评论

This works:

MyCollection.prototype.select = function (properties) {
   var self = this;

   return {
      where: function (conditions) {
         return _.chain(self.arrayOfObjects)
           .where(conditions)
           .map(function (result) {
              return _.pick(result, properties);
           })
           .value();
      }
   };
};

It allows me to query my collection like so:

var people = collection
             .select(['id', 'firstName'])
             .where({lastName: 'Mars', city: 'Chicago'});

I expected to be able to write the code like this, though:

MyCollection.prototype.select = function (properties) {
   var self = this;

   return {
      where: function (conditions) {
         return _.chain(self.arrayOfObjects)
           .where(conditions)
           .pick(properties);
           .value();
      }
   };
};

Lo-Dash documentation specifies the _.pick callback as "[callback] (Function|…string|string[]): The function called per iteration or property names to pick, specified as individual property names or arrays of property names." That led me to believe I could just supply the properties array, which would be applied to each item in arrayOfObjects that meets the conditions. What am I missing?

This works:

MyCollection.prototype.select = function (properties) {
   var self = this;

   return {
      where: function (conditions) {
         return _.chain(self.arrayOfObjects)
           .where(conditions)
           .map(function (result) {
              return _.pick(result, properties);
           })
           .value();
      }
   };
};

It allows me to query my collection like so:

var people = collection
             .select(['id', 'firstName'])
             .where({lastName: 'Mars', city: 'Chicago'});

I expected to be able to write the code like this, though:

MyCollection.prototype.select = function (properties) {
   var self = this;

   return {
      where: function (conditions) {
         return _.chain(self.arrayOfObjects)
           .where(conditions)
           .pick(properties);
           .value();
      }
   };
};

Lo-Dash documentation specifies the _.pick callback as "[callback] (Function|…string|string[]): The function called per iteration or property names to pick, specified as individual property names or arrays of property names." That led me to believe I could just supply the properties array, which would be applied to each item in arrayOfObjects that meets the conditions. What am I missing?

Share Improve this question edited Sep 10, 2014 at 15:24 Ilia Choly 18.6k14 gold badges94 silver badges164 bronze badges asked Sep 10, 2014 at 15:16 Fred MarsFred Mars 1311 gold badge4 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

http://lodash./docs#pick

It expects an Object as the first parameter, you're giving it an Array.

Arguments

1. object (Object): The source object.
2. ...
3. ...

I think this is the best you can do:

MyCollection.prototype.select = function (properties) {
   var self = this;

   return {
      where: function (conditions) {
         return _.chain(self.arrayOfObjects)
           .where(conditions)
           .map(_.partialRight(_.pick, properties))
           .value();
      }
   };
};

It doesn't work because _.pick expects an object, not a collection which is being passed through from the where function in your chain.

发布评论

评论列表(0)

  1. 暂无评论