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


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 query collection so:

var people = collection              .select(['id', 'firstname'])              .where({lastname: 'mars', city: 'chicago'}); 

i expected able write code 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 _.pick callback "[callback] (function|…string|string[]): function called per iteration or property names pick, specified individual property names or arrays of property names." led me believe supply properties array, applied each item in arrayofobjects meets conditions. missing?

http://lodash.com/docs#pick

it expects object first parameter, you're giving array.

arguments  1. object (object): source object. 2. ... 3. ... 

i think best 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();       }    }; }; 

Comments

Popular posts from this blog

python - Referencing Data From a 2D Histogram -

sql - Postgresql error: "failed to find conversion function from unknown to text" -

c# - Derived UserControl layout resets after build -