I need to implement a search query on a nested array of data. The user can input multiple search strings separated by a comma. I am storing those comma-separated strings as an array of strings and passing it to my filter function. The array of strings entered every time needs to match to 1 or more names of the subCategory array of objects.
The input string can be increased and the new property isActive:true can be attached to the object to which that string belongs, keeping the other unmatched objects with isActive:false added to them.
This is how my implementation goes so far. This implementation works fine in case I want to filter out only matched results.
    const filterData = (
            arrayOfData: Array<Object>,
            arrayOfString: Array<string>,
          ): Array<Object> =>
            arrayOfString &&
            arrayOfString.reduce((acc, current) => {
              const filteredData =
                arrayOfData &&
                arrayOfData
                  .map((category) => ({
                    ...category,
                    subCategory: category.subCategory.filter((data) =>
                      data.name
                        .toLowerCase()
                        .includes(current.toLowerCase()),
                    ),
                  }))
                  .map((data) => ({
                    ...data,
                    isActive: data.subCategory.length > 0
                  }));
          
              acc.push(...filteredData);
          
              return acc;
            }, []);
          
          export default filterData;
This seems to work fine partially. The acc.push(...filteredData) adds a new array every time a new string is added to the search, therefore increasing the array size a lot.
I am stuck on resolving this for quite a long, if anyone can help to implement this properly or in a better approach, much appreciated.
 
     
    