根据过滤器输入过滤对象数组

时间:2019-06-14 14:57:31

标签: javascript react-native

我希望能够过滤出我的平面清单,其中包含在用户区域内进行的活动,例如去电影院。

funtionality of the app

这是过滤器本身:

filter functionality

用户可以选择:

位置(位置标签):室内或室外,一无所有。

强度(范围标签):强烈(范围内)或不强烈(范围内)或什么都没有。

构图(精打细算):单独(Alleen)和/或朋友(Vrienden)和/或Coupple(伙伴)和/或有孩子的家庭(与Gezin遇见Kineren)和/或Group(抱怨) )AND / OR同事(Collega's)

因此可以忽略每个过滤器部分,例如可以使用合成和位置,而其他则不可以

然后按“显示(卡通)”按钮,将过滤后的结果显示在平面列表中。

我尝试创建此问题的代码并在数据库中对其进行测试根本无法正常工作,但结果却有所不同。

我试图过滤包含对象的数组,该数组可以正常工作,但结果却是错误的。

示例数据库数据:

[{
  Naam: 'Vue cinema',
  Location_Indoor: 0,
  Location_Outdoor: 1,
  Soort_Inspanning: 1,
  Soort_Ontspanning: 0,
  Samenstelling_Alleen: 1,
  Samenstelling_Vrienden: 0,
  Samenstelling_Stellen: 0,
  Samenstelling_Gezin_Kinderen: 1,
  Samenstelling_Groepen: 1,
  Samenstelling_Bedrijven: 1
}, {
  Naam: 'Shooting range',
  Location_Indoor: 0,
  Location_Outdoor: 1,
  Soort_Inspanning: 1,
  Soort_Ontspanning: 0,
  Samenstelling_Alleen: 0,
  Samenstelling_Vrienden: 1,
  Samenstelling_Stellen: 1,
  Samenstelling_Gezin_Kinderen: 1,
  Samenstelling_Groepen: 1,
  Samenstelling_Bedrijven: 0
}]
_filterFunction = (
  locationInput, // 0 == Indoor location, 1 == Outdoor location
  intensityInput, // 0 == Intensity Intense, 1 == Intensity Not Intense
  aloneSelected, // Alone option selected? BOOL
  friendsSelected, // Friends option selected? BOOL
  partnersSelected, // Coupple option selected? BOOL
  familyWithKidsSelected, // Family with kids selected? BOOL
  groupSelected, // Group selected? BOOL
  colleaugesSelected // Colleagues select? BOOL) => {
  let locationOutdoor = false;
  let intensityIntense = false;

  //6this.state.oldData == datasource
  const newData = this.state.oldData.filter(item => {
    const outdoorData = `${item.Locatie_Outdoor}`;
    const intensityData = `${item.Soort_Ontspanning}`;
    const compositionAloneData = `${item.Samenstelling_Alleen}`;
    const compositionFriendsData = `${item.Samenstelling_Vrienden}`;
    const compositionPartnersData = `${item.Samenstelling_Stellen}`;
    const compositionFamilyWithKidsData = `${item.Samenstelling_Gezin_Kinderen}`;
    const compositionGroupsData = `${item.Samenstelling_Groepen}`;
    const compositionColleagues = `${item.Samenstelling_Bedrijven}`;

    if (outdoorData == locationInput) {
      locationOutdoor = 1;
    } else if (intensityData == intensityInput) {
      intensityIntense = 1;
    }

    if (locationOutdoor && !intensityIntense) {
      console.log('alleen location');
      return locationInput == outdoorData;
    } else if (locationOutdoor &&
      intensityIntense &&
      !aloneSelected &&
      !friendsSelected &&
      !partnersSelected &&
      !familyWithKidsSelected &&
      !groupSelected &&
      !colleaugesSelected) { //if only location outdoor and intensityIntense == selected and not composition
      console.log('location en intensity');
      return (
        locationInput == outdoorData &&
        intensityInput == intensityData
      );
    } else if (locationOutdoor && intensityIntense &&
      (+aloneSelected == compositionAloneData ||
        +friendsSelected == compositionFriendsData ||
        +partnersSelected == compositionPartnersData ||
        +familyWithKidsSelected == compositionFamilyWithKidsData ||
        +groupSelected == compositionGroupsData ||
        +colleaugesSelected == compositionColleagues)) {
      console.log('location en intensity en composition');
      return (
        locationInput == outdoorData &&
        intensityInput == intensityData &&
        +aloneSelected == compositionAloneData //&& 
        +friendsSelected == compositionFriendsData &&
        +partnersSelected == compositionPartnersData &&
        +familyWithKidsSelected == compositionFamilyWithKidsData &&
        +groupSelected == compositionGroupsData &&
        +colleaugesSelected == compositionColleagues
      );

    } else if (!locationOutdoor && intensityIntense) {
      console.log('alleen intensity');
      return intensityInput == intensityData;
    } else if (!locationOutdoor && !intensityIntense) {
      console.log('niks');
      return this.state.oldData;
    } else {
      console.log('blabl')
    }
  }); this.setState({
    dataSource: newData
  }); this.props.navigation.setParams({
    searchVal: false
  });
};

很抱歉,如果没有完全解释或不清楚的地方,我正在研究2天,因此感到有点累。

0 个答案:

没有答案
相关问题