简体   繁体   English

我返回 `boolean` 但 Array.prototype.filter() 期望在箭头 function 的末尾返回一个值

[英]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 ->如果我从过滤器返回boolean有什么问题,我从 eslint 收到错误->

Array.prototype.filter() expects a value to be returned at the end of arrow function Array.prototype.filter() 期望在箭头结束时返回一个值 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 Array.prototype.filter() 期望在箭头结束时返回一个值 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.您的过滤器 function 中有三个if's ,但如果这些条件都不满足,仍然缺少要返回的回退值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Array.prototype.filter() 期望在箭头函数的末尾返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function Array.prototype.filter() 期望在箭头函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function array-callback-return react js Array.prototype.filter() 期望在箭头结束时返回一个值 function - react js Array.prototype.filter() expects a value to be returned at the end of arrow function Array.prototype.filter() 期望在函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of function array-callback-return Array.prototype.map() 期望在箭头函数结束时返回一个值 - Array.prototype.map() expects a value to be returned at the end of arrow function Array.prototype.map() 期望返回一个值,但不能根据其他问题返回 null,在箭头函数的末尾 - Array.prototype.map() expects a value to be returned, but can't return null as per other questions, at the end of arrow function 带有箭头函数回调的Array.prototype.filter()参数? (无此绑定) - Array.prototype.filter() argument with arrow function callback? (no this binding) Array.prototype.map() 期望来自箭头的返回值 function array-callback-return - Array.prototype.map() expects a return value from arrow function array-callback-return Array.prototype.map() 期望箭头函数的返回值 - Apollo 客户端 -Reactjs - Array.prototype.map() expects a return value from arrow function - Apollo client -Reactjs 警告:Array.prototype.reduce() 期望箭头 function 的返回值 - Warning: Array.prototype.reduce() expects a return value from arrow function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM