【问题标题】:Nested collection filter with lodash带有 lodash 的嵌套集合过滤器
【发布时间】:2016-11-28 08:51:41
【问题描述】:

我有一个像下面这样的集合

var 航班 = [{ 编号:1, 腿:[{ 运营商名称:'飞马' }] }, { 编号:2, 腿:[{ 运营商名称:'SunExpress' },{ 运营商名称:'SunExpress' }] }, { 编号:3, 腿:[{ 运营商名称:“飞马” },{ 运营商名称:'SunExpress' }] }]

我想过滤它,例如 carrierName = 'Pegasus' 然后我的结果是这样的

[{ 编号:1, 腿:[{ 运营商名称:'飞马' }] }, { 编号:3, 腿:[{ 运营商名称:“飞马” },{ 运营商名称:'SunExpress' }] }]

【问题讨论】:

    标签: javascript lodash


    【解决方案1】:

    只需检查某些航段是否包含运营商名称

    _.filter(flights, function(flight) {
        return _.chain(flight)
            .get('legs')
            .map('carrierName')
            .includes('Pegasus')
            .value()
    });
    

    检查 arr 值

    _.filter(flights, function(flight) {
        return _.chain(flight)
            .get('legs')
            .map('carrierName')
            .thru(function(names) {
                return _.every(valuesArr, function(val) { // _.some for OR, _.every for AND
                    return _.includes(names, val);
                });
            })
            .value()
    });
    

    【讨论】:

    • 它运行良好我如何使用数组 ex: ['Pegasus','SunExpress'] 进行包含?
    • @Mr.Ayanlar 我更新了答案,valuesArr - ['Pegasus','SunExpress']
    【解决方案2】:

    您可以使用Array.prototype.filter() 并使用Array.prototype.some() 检查每个子数组,看看它是否包含搜索词:

    var carrierNames = ['Pegasus', 'SunExpress'];
    
    flights.filter(function(item) {
      var predicate = this; // the carrierNames dictionary
    
      return item.legs.some(function(leg) {
        return predicate[leg.carrierName]; // see if carrierName exists in the dictionary
      });
    }, carrierNames.reduce(function(obj, term) { // create a dictionary of carrierNames
      obj[term] = true;
      return obj;
    }, Object.create(null)));
    

    var flights = [{
        id: 1,
        legs:[{
            carrierName:'Pegasus' 
        }]
    }, {
        id: 2,
        legs:[{
            carrierName: 'SunExpress'
        },{
            carrierName: 'SunExpress'
        }]
    }, {
        id: 3,
        legs:[{
            carrierName: 'Pegasus'
        },{
            carrierName: 'SunExpress'
        }]
    }];
    
    var carrierNames = ['Pegasus', 'SunExpress'];
    
    var result = flights.filter(function(item) {
      var predicate = this;
      
      return item.legs.some(function(leg) {
        return predicate[leg.carrierName];
      });
    }, carrierNames.reduce(function(obj, term) {
      obj[term] = true;
      return obj;
    }, Object.create(null)));
    
    console.log(result);

    与使用ES6箭头函数和参数destructuring的逻辑相同:

    const result = flights.filter(function({ legs }) {
      const predicate = this;
      return legs.some(({ carrierName }) => predicate.has(carrierName)); // keep if carrierName is in the Set
    }, new Set(carrierNames)); // create a Set from the carrierNames
    

    const flights = [{
      id: 1,
      legs: [{
        carrierName: 'Pegasus'
      }]
    }, {
      id: 2,
      legs: [{
        carrierName: 'SunExpress'
      }, {
        carrierName: 'SunExpress'
      }]
    }, {
      id: 3,
      legs: [{
        carrierName: 'Pegasus'
      }, {
        carrierName: 'SunExpress'
      }]
    }];
    
    const carrierNames = ['Pegasus', 'SunExpress'];
    
    const result = flights.filter(function({ legs }) {
      const predicate = this;
      return legs.some(({ carrierName }) => predicate.has(carrierName));
    }, new Set(carrierNames));
    
    console.log(result);

    【讨论】:

    • 如何使用数组 ex: ['Pegasus','SunExpress'] 进行包含?
    • 它应该包含数组中的所有术语还是仅包含其中一个?你在使用 ES6 吗?
    【解决方案3】:

    谢谢大家。我用如下方式解决了它

    var 航空公司 = ["Pegasus",SunExpress"]; var 结果 = _.filter(航班,功能(项目){ 返回 _.some(item.legs, function(leg) { return _.includes(airlines, leg.carrierName); }); });

    【讨论】:

      【解决方案4】:

      使用 ES6,您可以使用单行进行过滤,使用 Array#filter 和 Array#some。

      var flights = [{ id: 1, legs: [{ carrierName: 'Pegasus' }] }, { id: 2, legs: [{ carrierName: 'SunExpress' }, { carrierName: 'SunExpress' }] }, { id: 3, legs: [{ carrierName: 'Pegasus' }, { carrierName: 'SunExpress' }] }],
          search = new Set(['Pegasus', 'SunExpress']);
          result = flights.filter(a => a.legs.some(b => search.has(b.carrierName)));
      
      console.log(result);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

      • 我暂时不需要 ES6。我怎样才能使用 lodash?
      猜你喜欢
      • 2021-02-21
      • 2016-03-23
      • 1970-01-01
      • 2017-08-03
      • 2016-02-25
      • 2017-01-11
      • 1970-01-01
      • 2017-06-15
      • 2020-09-05
      相关资源
      最近更新 更多