简体   繁体   English

带有lodash的嵌套集合过滤器

[英]Nested collection filter with lodash

I have a collection like as below 我有一个如下的收藏

var flights = [{
    id: 1,
    legs:[{
        carrierName:'Pegasus' 
    }]
}, {
    id: 2,
    legs:[{
        carrierName: 'SunExpress'
    },{
        carrierName: 'SunExpress'
    }]
}, {
    id: 3,
    legs:[{
        carrierName: 'Pegasus'
    },{
        carrierName: 'SunExpress'
    }]
}]

I want to filter it for example carrierName = 'Pegasus' then my result is like this 我想过滤它,例如,carrierName ='Pegasus',那么我的结果是这样的

[{
    id: 1,
    legs:[{
        carrierName:'Pegasus' 
    }]
}, {
    id: 3,
    legs:[{
        carrierName: 'Pegasus'
    },{
        carrierName: 'SunExpress'
    }]
}]

You can use Array.prototype.filter() and check each sub array using Array.prototype.some() to see if it includes the search term: 您可以使用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); 

And the same logic using ES6 arrow functions and parameters destructuring : 并使用ES6箭头功能和参数进行相同的逻辑解构

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

just check if some legs of flight contains carrierName 只需检查飞行的某些航段是否包含carrierName

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

for check arr of values 用于检查值的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()
});

With ES6, you could use a single line for filtering, using Array#filter and Array#some . 使用ES6,您可以使用Array#filterArray#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; } 

thank you everybody. 谢谢大家。 I solved it with like as below 我如下解决了

var airlines = ["Pegasus",SunExpress"];
    var result = _.filter(flights, function(item) {
                        return _.some(item.legs, function(leg) {
                            return _.includes(airlines, leg.carrierName);
                        });
                    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM