Home > Net >  i return `boolean` but Array.prototype.filter() expects a value to be returned at the end of arrow f
i return `boolean` but Array.prototype.filter() expects a value to be returned at the end of arrow f

Time:11-17

What's wrong if i return boolean from filter it doesn't matter i get an Error from eslint ->

Array.prototype.filter() expects a value to be returned at the end of arrow function

code example ->

let filteredData = useMemo(() => {
            let childIds = [];
            let parentIds = [];
            if (customSearch || !searchQuery) {
                return data;
            } else {
                //finding childIds and pushing in childIds
                for (let item of data) {
                    if (
                        item[labelKey] &&
                        item[labelKey].toString().toLowerCase().includes(searchQuery.toString().toLowerCase())
                    ) {
                        if (item.childId) childIds.push(item.childId);
                        if (item.parentId) parentIds.push(item.parentId);
                    }
                }
                //returning only groupsIds that not match in childIds
                parentIds = parentIds.filter((e) => !(childIds.indexOf(e) >= 0));
    
                return data.filter((item) => {
                    //return groupParents of items that match
                    for (let i of childIds) {
                        if (item.parentId && item.parentId === i) return true;
                    }
                    //return all group that match only group title
                    for (let i of parentIds) {
                        if (item.parentId === i || item.childId === i) return true;
                    }
                    if (
                        item[labelKey] &&
                        item[labelKey].toString().toLowerCase().includes(searchQuery.toString().toLowerCase()) &&
                        !item.parentId
                    ) {
                        return true;
                    }
                });
            }
        }, [data, labelKey, searchQuery, customSearch, options]);

what do you think what's wrong

CodePudding user response:

Array.prototype.filter() expects a value to be returned at the end of arrow function

You have three if's in your filter function, but still missing a fallback value to be returned if none of those conditions fullfills.

Just add some fallback return (before closing the callback function) if none of those conditions are true.

return data.filter((item) => {
   for (let i of childIds) {
      if (item.parentId && item.parentId === i) return true;
   }
   for (let i of parentIds) {
      if (item.parentId === i || item.childId === i) return true;
   }
   if (
      item[labelKey] &&
                    item[labelKey].toString().toLowerCase().includes(searchQuery.toString().toLowerCase()) &&
      !item.parentId
   ) {
      return true;
   }

   return true; // fallback (or false)
});
  • Related