At underscore js, Can I get multiple columns with pluck method after input where method as linq select projection

后端 未结 7 1172
小蘑菇
小蘑菇 2021-01-30 12:59
var people = [
    {firstName : \"Thein\", city : \"ny\", qty : 5},
    {firstName : \"Michael\", city : \"ny\", qty : 3},
    {firstName : \"Bloom\", city : \"nj\", qty         


        
7条回答
  •  悲哀的现实
    2021-01-30 13:49

    My understanding is the author of the question wants to take an array of objects with many properties and strip each object down to a small list of properties.

    There are myriad ways of doing so with _ , but I like this way best. Pass in an empty result object which will be "this" inside the function. Iterate with _each , and _pick the fields you want:

    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);
    

提交回复
热议问题