Pasindu Weerakoon
Pasindu Weerakoon

Reputation: 628

React-Native array object filtering not working as expected

I'm working on the react-native project and I have an array object which is coming from backend.

here is the sample of an Array.

 {
   "2010":[
      {
         "id":1243,
         "eventName":"sample_01",
         "categoryType":"CUSTOM_NOTES",
         "tags":"tag19",
         "lastModified":"2022-10-04T14:31:32Z",
         "attachments":[
            
         ]
      }
   ],
   "2022":[
      {
         "id":1244,
         "eventName":"sample_02",
         "categoryType":"CUSTOM_NOTES",
         "tags":"tag1, tag12, tag3, tag52, tag19",
         "lastModified":"2022-10-04T14:31:32Z",
         "attachments":[
            
         ]
      },
      {
         "id":1245,
         "eventName":"hello_03",
         "categoryType":"CUSTOM_NOTES",
         "tags":"tag1, tag12",
         "lastModified":"2022-10-04T14:31:32Z",
         "attachments":[
            
         ]
      }
   ]
}

In this array, it has some tags which were provided by the user previously (ex: "tags":"tag1, tag12"). There can be multiple or single tags in the array object. Using those tags or tags user should be able to filter the items in the array.

But my code is not working for the above behavior and it filters the elements with a tag, which is selected at last by the user.

Please help me to find an answer. Thanks in advance.

My code for the filtering as follows.

FilterByTagsOrAttachements = (advancedFilterData) => {
    let advancedFilterFn = {};
    if (advancedFilterData[0].filterType === "Tag") {
      for (let advancedItem of advancedFilterData) {
        for (const itemTag of advancedItem.data) {
          advancedFilterFn = (a) => {
            for (let tag of a.tags.split(',')) {
              return isEqual(tag, itemTag);
            }
          };
        }
      }
    } else if (advancedFilterData[1].filterType === "Attachments") {
      console.log(JSON.stringify(advancedFilterData[1].data));
    }
    this.onCreateAdvancedFilterObject(advancedFilterFn);
  }

onCreateAdvancedFilterObject = (advancedFilterFn) => {
    this.setState(state => {
      const events = {};
      for (const [year, items] of Object.entries(state.events)) {
        events[year] = items.slice().filter(advancedFilterFn);
      }
      return { events }
    })
  }

and advancedFilterData array is as follws.

advancedFilterData => [{"filterType":"Tag","data":["tag1","tag12"]},{"filterType":"Attachments","data":[]}]

Upvotes: 0

Views: 197

Answers (1)

Apostolos
Apostolos

Reputation: 10463

I guess you need to store all the filters inside an array and then use this array to check if it includes all the elements. Also, since you have , , you should split with , unless you handle it accordingly inside your isEqual method.

FilterByTagsOrAttachements = (advancedFilterData) => {
    let advancedFilterFn = {};
    let filterArray = []
    if (advancedFilterData[0].filterType === "Tag") {
      for (let advancedItem of advancedFilterData) {
        filterArray = filterArray.concat(advancedItem.data)
      }
      
     advancedFilterFn = (a) => {
        for (let tag of a.tags.split(', ')) {
          return filterArray.includes(tag);
        }
     };
    } else if (advancedFilterData[1].filterType === "Attachments") {
      console.log(JSON.stringify(advancedFilterData[1].data));
    }
    this.onCreateAdvancedFilterObject(advancedFilterFn);
  }

Upvotes: 1

Related Questions