简体   繁体   中英

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

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

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)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM