在下划线js,我可以得到多个采用pluck方法的列后,方法作为linqselect投影

var people = [ {firstName : "Thein", city : "ny", qty : 5}, {firstName : "Michael", city : "ny", qty : 3}, {firstName : "Bloom", city : "nj", qty : 10} ]; var results=_.pluck(_.where(people, {city : "ny"}), 'firstName'); 

例如:我需要firstNameqty

要投影到多个属性,您需要map ,而不是pluck:

 var results = _.map( _.where(people, {city : "ny"}), function(person) { return { firstName: person.firstName, qty: person.qty }; } ); 

[{ “名字”: “登”, “数量”:5},{ “名字”: “迈克尔”, “数量”:3}]

(小提琴)

请注意,如果你想,你可以创build一个helper方法“pluckMany”,它和可变参数pluck一样。

 // first argument is the source array, followed by one or more property names var pluckMany = function() { // get the property names to pluck var source = arguments[0]; var propertiesToPluck = _.rest(arguments, 1); return _.map(source, function(item) { var obj = {}; _.each(propertiesToPluck, function(property) { obj[property] = item[property]; }); return obj; }); }; 

您可以使用_.mixin函数向_命名空间添加“pluckMany”函数。 使用这个你可以简单的写下:

 var results = _.chain(people).where({city : "ny"}).pluckMany( "firstName", "qty").value(); 

(小提琴)

TL; DR使用:

 var results = _.chain(people) .where({ city: "ny" }) .map(_.partialRight(_.pick, 'firstName', 'qty')) .value(); 

但请阅读解释,因为我觉得find这个解决scheme的过程比实际的答案更有趣。


一般的模式是(它也适用于lodash ):

_.map(array, function(obj) { return _.pick(obj, 'x', 'y', 'z'); });

给定这个转换集合中每个元素的通用map函数,可以有多种方法来适应您的特定情况(保证map的灵活性,这是函数式程序的一个非常基本的构build块)。

让我来介绍几种实现我们解决scheme的方法:

 var _ = require('lodash'); // @lodash 2.4.1 at the time of writing // use underscore if you want to, but please see http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore /* la data */ var people = [{ firstName: "Thein", city: "ny", qty: 5 }, { firstName: "Michael", city: "ny", qty: 3 }, { firstName: "Bloom", city: "nj", qty: 10 }]; /* OPTION1 : mixin' with _ */ _.mixin({ pluckMany: function() { var array = arguments[0], propertiesToPluck = _.rest(arguments, 1); return _.map(array, function(item) { /* Alternative implementation 1.1 * ------------------------------ * Taken from @mMcGarnagle answer * _each is easy to understand here, * but has to modify the variable `obj` from a closure * I try to avoid that for trivial cases like this one. */ var obj = {}; _.each(propertiesToPluck, function(property) { obj[property] = item[property]; }); return obj; /* Alternative implementation 1.2 * ------------------------------ * Rewrite the previous code, * by passing the accumulator (previously`obj`, but really it is an object that accumulates the result being constructed) across function calls. * This construction is typical of the `reduce` function, closer to a functionnal programming style. */ return _.reduce(propertiesToPluck, function(obj, property) { obj[property] = item[property]; return obj; }, {}); /* Alternative implementation 1.3 * ------------------------------ * If we are already using lodash/underscore, * then let's use the `pick` function ! I also included an example of `flatten` here */ return _.pick(item, _.flatten(propertiesToPluck, true)); /* Alternative implementation 1.4 * ------------------------------ * But really flatten is not needed. */ return _.partial(_.pick, item).apply(null, propertiesToPluck); }); } }); /* Let's use our mixed function ! * Since we call several _ functions on the same object * it is more readable to chain the calls. */ var results = _.chain(people) .where({ city: "ny" }) .pluckMany('firstName', 'qty') .value(); /* OPTION 2 : without mixing our code with lodash/underscore */ var results = _.chain(people) .where({ city: "ny" }) .map(_.partialRight(_.pick, 'firstName', 'qty')) .value(); console.log(results); 

如果你喜欢这种用underscorelodash编写代码的方式,我强烈build议你看看函数式编程 ,因为这种编写风格以及许多函数( mapreduce等等)都来自这里。

注意:这显然是下划线中的常见问题: https : //github.com/jashkenas/underscore/issues/1104

如果这些被省略了下划线/ lodash,这显然不是偶然的:“可组合性好于特征”。 你也可以说do one thing and do it well 。 这也是为什么_.mixin存在。

我的理解是这个问题的作者想要获取具有许多属性的对象的数组,并将每个对象剥离到一个小的属性列表。

_有很多方法,但我最喜欢这种方式。 在函数内部传入一个空的结果对象即“this”。 用_each迭代,然后select你想要的字段:

 var myObjects = [ { "first" : "eric", "last" : "gumbo", "code" : "x482" }, { "first" : "john", "last" : "dinkman", "code" : "y9283" } ]; var result = []; _.each( myObjects, function(itm) { this.push(_.pick(itm,"first","code")) }, result ); console.log(result); 

是的,我希望pluck有一个传递数组的选项,但在此期间,你可以这样做:

 function pluckFields(arr, fields) { return _.map(arr, function(item) { return _.pick(item, fields) }) } 

这一个线性可能会保存一些行:

 var results=_.pick(_.where(people, {city : "ny"}), 'firstName', 'qty'); 

我们不必使用采摘,也可以省略。

 var result = _.map(people, function(person) { return _.omit(person, 'city'); });