0

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":[]}]
1
  • Should your tag split by , instead of , ? Commented Oct 5, 2022 at 7:53

1 Answer 1

1

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);
  }
Sign up to request clarification or add additional context in comments.

3 Comments

can you upload a dummy project with your code here? snack.expo.dev
Hi, Thank you for your answer. Still, I have a similar issue. Assume the tag which I want to find, is in the 2nd place of the tags. That is not in the filter list. ex : "tags":"tag1, tag12" - if I filter by "tag1". It is not appearing on the list. But if I search for 1st item ('tags') it will be in the list.
Hi here is the sample : snack.expo.dev/NEmnADpcP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.