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

后端 未结 7 1165
小蘑菇
小蘑菇 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:57

    YAAUu-Yep Another Answer Using underscore...

    // use a proper browser to run this code snippet, a browser that is es6-compliant
    let people = [{
        firstName: "Thein",
        city: "ny",
        qty: 5
      },
      {
        firstName: "Michael",
        city: "ny",
        qty: 3
      },
      {
        firstName: "Bloom",
        city: "nj",
        qty: 10
      }
    ];
    // either you pick the properties you want 
    let picking = _.iteratee((person) => _(person).pick("firstName", "city"));
    // either you omit the properties you do not want
    let omitting = _.iteratee((person) => _(person).omit("qty"));
    // create the filter by city
    let living = (people, city) => _(people).where({
      "city": city
    });
    // put the "filter by city" into a mixin (as I assume it would be used again & again)
    _.mixin({
      living: living
    });
    // do the thing (twice), 
    // these chaining methods could be done into a mixin as well
    console.log("results by picking properties:", _(people).chain().living("ny").map(picking).value());
    console.log("results by omitting properties:", _(people).chain().living("ny").map(omitting).value());

提交回复
热议问题