[英]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.