- Самое простое решение: просто жестко закодируйте все поля
let boats = [
{Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
{Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
{Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
{Price: 396900, BrandName: null , BoatYear: 2020},
{Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 138700, BrandName: "HR" , BoatYear: 2020},
{Price: 178900, BrandName: "HR" , BoatYear: 2020},
{Price: 348900, BrandName: "HR" , BoatYear: 2020},
{Price: 285800, BrandName: "HR" , BoatYear: 2020},
{Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
{Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
{Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
{Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
{Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 247900, BrandName: "SILVER" , BoatYear: 2020}
];
const expected_selected = {
BoatYear : 2020,
BrandName: 'LINDER',
Price : { min: 0, max: 138000 },
}
const filter_by = filters => item => {
if (item.BoatYear === undefined || item.BoatYear !== filters.BoatYear ) return false
if (item.BrandName === undefined || item.BrandName !== filters.BrandName ) return false
if (item.Price < filters.Price.min || item.Price > filters.Price.max) return false
return true
}
boats = boats.filter(filter_by(expected_selected))
console.log(`Results: ${JSON.stringify(boats)}`);
Или используйте мин / макс везде
let boats = [
{Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
{Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
{Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
{Price: 396900, BrandName: null , BoatYear: 2020},
{Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 138700, BrandName: "HR" , BoatYear: 2020},
{Price: 178900, BrandName: "HR" , BoatYear: 2020},
{Price: 348900, BrandName: "HR" , BoatYear: 2020},
{Price: 285800, BrandName: "HR" , BoatYear: 2020},
{Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
{Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
{Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
{Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
{Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
]
const expected_selected = {
BoatYear : { min: 2020 , max: 2020 },
BrandName: { min: 'LINDER', max: 'LINDER' },
Price : { min: 0 , max: 138000 },
}
const filter_by = filters => item => {
for (var key in filters) {
if (item[key] === undefined) return false
if (item[key] < filters[key].min || item[key] > filters[key].max) return false
}
return true
}
boats = boats.filter(filter_by(expected_selected))
console.log(`Results: ${JSON.stringify(boats)}`);
Или проверьте тип поля
selected
(в данном случае
Array.isArray
, в случае
{min,max}
это будет
instanceof
)
let boats = [
{Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
{Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
{Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
{Price: 396900, BrandName: null , BoatYear: 2020},
{Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 138700, BrandName: "HR" , BoatYear: 2020},
{Price: 178900, BrandName: "HR" , BoatYear: 2020},
{Price: 348900, BrandName: "HR" , BoatYear: 2020},
{Price: 285800, BrandName: "HR" , BoatYear: 2020},
{Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
{Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
{Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
{Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
{Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
]
const expected_selected = {
BoatYear : 2020,
BrandName: 'LINDER',
Price : [ 0, 138000 ],
}
const filter_by = filters => item => {
for (var key in filters) {
if (item[key] === undefined) return false
if (Array.isArray(filters[key])) {
if(item[key] < filters[key][0] || item[key] > filters[key][1]) return false
} else if (item[key] !== filters[key]) return false
}
return true
}
boats = boats.filter(filter_by(expected_selected))
console.log(`Results: ${JSON.stringify(boats)}`);
Или еще лучше, используйте OOP
let boats = [
{Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
{Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
{Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
{Price: 396900, BrandName: null , BoatYear: 2020},
{Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 138700, BrandName: "HR" , BoatYear: 2020},
{Price: 178900, BrandName: "HR" , BoatYear: 2020},
{Price: 348900, BrandName: "HR" , BoatYear: 2020},
{Price: 285800, BrandName: "HR" , BoatYear: 2020},
{Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
{Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
{Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
{Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
{Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
{Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
{Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
]
class MinMax {
constructor(min, max) { this.min = min, this.max = max }
check(val) { return val >= this.min && val <= this.max }
}
class Eq {
constructor(val) { this.val = val }
check(val) { return val === this.val }
}
var expected_selected = {
BoatYear : new Eq(2020),
BrandName: new Eq('LINDER'),
Price : new MinMax(0, 138000)
}
const filter_by = filters => item => {
for (var key in filters) {
if (item[key] === undefined) return false
if (filters[key].check(item[key]) === false) return false
}
return true
}
boats = boats.filter(filter_by(expected_selected))
console.log(`Results: ${JSON.stringify(boats)}`);
Таким образом, вы можете расширить свои фильтры, добавив новые классы без изменения функции filter_by
.