[英]Nested collection filter with lodash
我有一个如下的收藏
var flights = [{ id: 1, legs:[{ carrierName:'Pegasus' }] }, { id: 2, legs:[{ carrierName: 'SunExpress' },{ carrierName: 'SunExpress' }] }, { id: 3, legs:[{ carrierName: 'Pegasus' },{ carrierName: 'SunExpress' }] }]
我想过滤它,例如,carrierName ='Pegasus',那么我的结果是这样的
[{ id: 1, legs:[{ carrierName:'Pegasus' }] }, { id: 3, legs:[{ carrierName: 'Pegasus' },{ carrierName: 'SunExpress' }] }]
您可以使用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箭头功能和参数进行相同的逻辑解构 :
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);
只需检查飞行的某些航段是否包含carrierName
_.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()
});
使用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; }
谢谢大家。 我如下解决了
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.