Home > Enterprise >  my filter only displays elements with first condition
my filter only displays elements with first condition

Time:06-17

I don't really know the best way to explain this: I have a list with bases and each base has a list of connectors (it could be one or multiple), I made a filter to filter my bases by connectors, here's what my method looks like:

calcBaseList: function() {
  let tmp = [];
  if (this.filterConnector.length > 0) {
    this.listBase.forEach((base) => {
      if (this.filterConnector.includes(base.connectors[0].standard)) {
        tmp.push(base);
      }
    });
  } else {
    tmp = this.listBase;
  }
  this.filtredBase = tmp;
},

The problem is, when I wanna filter let's say with "connector_base_3" and I have one of my bases that has the "connector_base_3" as one of the connectors but it's not the first one on the list, the base doesn't show up on my filtered list. I tried changing base.connectors[0].standard with base.connectors.standard or base.connectors but it doesn't filter anything in that case

Sorry if my explanation is a bit confusing. Does anyone know how to fix the issue here?

CodePudding user response:

You should check all connectors in your filter - not just the first one!

calcBaseList() 
{
  let tmp = [];
  if (this.filterConnector.length > 0) 
  {
    tmp = this.listBase.filter((base) => base.connectors.some(connector => this.filterConnector.includes(connector.standard)));
  } 
  else 
  {
    tmp = this.listBase;
  }
  this.filtredBase = tmp;
},
  • Related