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":[]}]
,instead of,?