【发布时间】:2020-08-13 13:56:40
【问题描述】:
我在 VueJS (It's a app with drop downs and ranges @ codepen) 中使用交互式搜索过滤器时遇到了困难
一艘船有 BrandName、BrandYear、Price...,我可以使用 selected = {...} 过滤这些信息,但我想知道如何充分利用下面的 if-statement ,通过expected_selected = {...}来识别价格并检查最小值/最大值并返回结果
我正在寻找有关如何过滤最小/最大值以及以下代码的解释/帮助。
目标是输入最小值和最大值以及一个或多个匹配的键值
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)}`);
【问题讨论】:
-
你希望从你的 sn-p 得到什么输出?
-
@Nikolas,想要的输出是一个对象数组,目前在 Vue-app 中它是一个
computed value
标签: json vue.js filter conditional-statements filtering