![](/img/trans.png)
[英]Array.prototype.filter() expects a value to be returned at the end of arrow function
[英]i return `boolean` but Array.prototype.filter() expects a value to be returned at the end of arrow function
如果我从过滤器返回boolean
有什么问题,我从 eslint 收到错误->
Array.prototype.filter() 期望在箭头结束时返回一个值 function
代码示例 ->
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]);
你觉得怎么了
Array.prototype.filter() 期望在箭头结束时返回一个值 function
您的过滤器 function 中有三个if's
,但如果这些条件都不满足,仍然缺少要返回的回退值。
如果这些条件都不成立,只需添加一些后备返回(在关闭回调函数之前)。
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.