I'm struggling with a interactive search filter in VueJS (It's a app with drop downs and ranges @ codepen)
A boat has BrandName, BrandYear, Price... which I've been able to filter through using selected = {...}, but I wonder how to make the best usage of this if-statement below, to identify the Price and check min/max and return results by passing expected_selected = {...}
I'm looking for explanation/help on how I can filter a min/max value together with the following code.
Goal is to input a minimuma and maximum value together with one or more matched key values
var 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,
}];
var selected = {
BoatYear: 2020,
BrandName: "LINDER"
};
var expected_selected = {
BoatYear: 2020,
BrandName: 'LINDER',
Price: [0, 138000] // min , max
}
boats = boats.filter(function(item) {
for (var key in selected) {
if (item[key] === undefined || item[key] != selected[key]) return false;
}
return true;
});
console.log(`Results: ${JSON.stringify(boats)}`);
computed value