我正在搜索对象数组。如果 item
的状态代码为 1
,我将推送到 openIssue
,如果是 3
,则推送到 inProgressIssue
。
下面是我的代码:
openIssue = [];
inProgressIssue = [];
issuesNotCompletedInCurrentSprint.filter((item) => {
if (item.statusId === '1') {
openIssue.push(item);
} else if (item.statusId === '3') {
inProgressIssue.push(item);
}
});
我收到了代码审查意见:
Illegal use of filter
请告诉我这里做错了什么。
最佳答案
你得到那个错误是因为过滤器期望你返回 bool,过滤器总是这样做。是的过滤器是一个迭代,但返回值应该是 bool 值,这就是它的制作方式, 我建议您使用这样的过滤器,这非常简单。你可以使用 foreach也不会返回任何东西。
openIssue = issuesNotCompletedInCurrentSprint.filter(a => a.statusId === '1');
inProgressIssue = issuesNotCompletedInCurrentSprint.filter(a => a.statusId === '3');
在这里filter将返回 statusid
并返回 fitlered 的值
let issuesNotCompletedInCurrentSprint = [{
statusId: '1',
name: 'asfadsf'
}, {
statusId: '2',
name: 'asfadsf11'
}, {
statusId: '3',
name: 'asfadsf6636'
}];
let openIssue = [];
let inProgressIssue = []
openIssue = issuesNotCompletedInCurrentSprint.filter(a => a.statusId === '1');
inProgressIssue = issuesNotCompletedInCurrentSprint.filter(a => a.statusId === '3');
console.log('open', openIssue, 'inprogress', inProgressIssue)
关于angular - 非法使用过滤器 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54006059/