我有一个如下所示的集合
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' }] }]
答案 0 :(得分:1)
您可以使用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);
答案 1 :(得分:1)
检查一些航班的行程是否包含carrierName
_.filter(flights, function(flight) {
return _.chain(flight)
.get('legs')
.map('carrierName')
.includes('Pegasus')
.value()
});
用于检查值的值
_.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()
});
答案 2 :(得分:0)
使用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; }

答案 3 :(得分:0)
var airlines = ["Pegasus",SunExpress"]; var result = _.filter(flights, function(item) { return _.some(item.legs, function(leg) { return _.includes(airlines, leg.carrierName); }); });