简体   繁体   English

过滤多个输入字段

[英]Filtering multiple input fields

I'm trying to create a filter with javascript with 4 input fields so I'm guessin 16 combinations of possible searches. 我正在尝试使用具有4个输入字段的javascript创建过滤器,因此我猜测可能的搜索有16种组合。 I can search all 4 at once or 1 input at a time but for some reason when I add other statements I get wierd results. 我可以一次搜索全部4个,也可以一次搜索1个输入,但是由于某种原因,当我添加其他语句时,结果却很奇怪。 Is there a better way to implement a filter? 有没有更好的方法来实现过滤器?

var unfilteredFloorplans = floorplanJSON.floorplanData;

filteredFloorplans = [];

for (var i = 0; i < unfilteredFloorplans.length; i++) {

    if (unfilteredFloorplans[i].city == req.body.cityName && 
        unfilteredFloorplans[i].building == req.body.buildingName && 
        unfilteredFloorplans[i].bedrooms == req.body.minBedroom && 
        unfilteredFloorplans[i].baths == req.body.maxBathroom) {

           console.log(unfilteredFloorplans[i].city);
           filteredFloorplans.push(unfilteredFloorplans[i]);
    } 
}

So now I need to write 15 more if statements? 所以现在我需要再写15条if语句? Rather than copy them in I'd like to ask if this is correct and does anyone know how you could implement this with a switch statement? 我想问一下这是否正确,而不是复制它们,是否有人知道如何使用switch语句实现此目的?

Edit: And when I say 15 more statements I mean one for if they just pick city, andother if they pick city and bedrooms etc. It just seems inefficient 编辑:当我再说15条陈述时,我的意思是说如果他们只是选择城市,而如果他们选择城市和卧室等,那似乎效率很低

A minimal fix would be to combine your "and" with "or", but note how this turns the code into a hard-to-read mess: 一个最小的解决方案是将您的“和”与“或”组合在一起,但是请注意这如何将代码变成难以理解的混乱:

var unfilteredFloorplans = floorplanJSON.floorplanData;

filteredFloorplans = [];

for (var i = 0; i < unfilteredFloorplans.length; i++) {

    if ((req.body.cityName == '' || unfilteredFloorplans[i].city == req.body.cityName) && 
        (req.body.buildingName == '' || unfilteredFloorplans[i].building == req.body.buildingName) && 
        (req.body.minBedroom == '' || unfilteredFloorplans[i].bedrooms == req.body.minBedroom) && 
        (req.body.maxBathroom == '' || unfilteredFloorplans[i].baths == req.body.maxBathroom)) {

           console.log(unfilteredFloorplans[i].city);
           filteredFloorplans.push(unfilteredFloorplans[i]);
    } 
}

(BTW, this looks like a good exercise for combining conjunctions with disjunctions.) (顺便说一句,这看起来像是将合取与析取相结合的好方法。)

Edit I'd recommend to put the filtering into a separate function, and to introduce an additional helper function. 编辑我建议将过滤放到一个单独的函数中,并引入一个附加的辅助函数。 Also, use a more consistent naming and use "===" instead of "==". 另外,使用更一致的命名并使用“ ===”代替“ ==”。

function filterByEquality(formValue, dataValue) {
    if (formValue === '') return true;
    if (formValue === dataValue) return true;
    return false;
}

function filterFloorplan(form, data) {
    if (!filterByEquality(form.city, data.city)) return false;
    if (!filterByEquality(form.building, data.building)) return false;
    if (!filterByEquality(form.minBedrooms, data.bedrooms)) return false;
    if (!filterByEquality(form.maxBathrooms, data.bathrooms)) return false;
    return true;
}

var unfilteredFloorplans = floorplanJSON.floorplanData;
filteredFloorplans = [];
for (var i = 0; i < unfilteredFloorplans.length; i++) {
    if (filterFloorplan(req.body, unfilteredFloorplans[i]);
        console.log(unfilteredFloorplans[i].city);
        filteredFloorplans.push(unfilteredFloorplans[i]);
    } 
}

You can reduce this code even further by learning about the Array.filter method. 您可以通过了解Array.filter方法来进一步减少此代码。 And you should fix the bug where for some fields should use ">=" or ">=" instead of "===". 并且您应该修复该错误,该错误对于某些字段应使用“> =”或“> =”而不是“ ===”。 But I'll leave those things as an exercise. 但是,我会将这些内容保留为练习。

Here's a simplified example of what your code may look like (in this example, I hardcoded the values representing the input choices): 这是代码看起来像的简化示例(在此示例中,我对代表input选择的值进行了硬编码):

var unfilteredFloorplans = [{
  city: 'NY',
  building: 'A',
  bedrooms: 2,
  baths: 1,
}];


var filteredFloorplans = unfilteredFloorplans.filter(
  function(el) {
    return el.city === 'NY' && el.building === 'A' && el.bedrooms >= 1 && el.baths >= 1;
  }
);

console.log(filteredFloorplans);

The anonymous function being called inside the filter can be replaced with a named function like so: 可以在filter内部调用的匿名函数替换为命名函数,如下所示:

function filterFloorplans(floorplan) {
  return floorplan.city === 'NY' && floorplan.building === 'A' && floorplan.bedrooms >= 1 && floorplan.baths >= 1;
}

var filteredFloorplans = unfilteredFloorplans.filter(filterFloorplans);

You'll likely want to use this route since you can have any combination of the 4 input choices. 您可能要使用此路由,因为您可以将4个输入选项进行任意组合。 As such, you'll want the filterFloorplans function to be "built-up" from other, smaller checks: 因此,您需要从其他较小的检查中“构建” filterFloorplans函数:

function testCity(userInputCity, floorplanCity) {
    return userInputCity ? userInputCity === floorplanCity : true;
}

function filterFloorplans(floorplan) {
  return testCity('NY', floorplan.city) && floorplan.building === 'A' && floorplan.bedrooms >= 1 && floorplan.baths >= 1;
}

This should be enough to get you started; 应该足以让您入门。 feel free to comment if you get stuck 如果遇到困难,请随时发表评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM