For-loop inside filter 方法不能按预期工作
For-loop inside filter method doesn't work as expected
我正在使用 filter
方法过滤出一个数组,如下所示:
this.gridOptions.api.setRowData(
this.arrayToFilter.filter(obj => obj.status !== "Declined")
);
上面的代码工作正常,但是当我尝试遍历 table 的所有列字段时它不起作用,这是我的代码:
this.gridOptions.api.setRowData(this.arrayToFilter.filter(function(item) {
for(var i = 0; i < columns.length; i++) {
var temp = columns[i].headerName
if(item[temp] === 'Declined')
return false
return true
}
}));
我在循环中做错了什么?
改用map迭代
this.gridOptions.api.setRowData(this.arrayToFilter.filter(function(item) {
let checkDeclined = true;
columns.map(column => {
let temp = columns[i].headerName
if(item[temp] === 'Declined')
checkDeclined = false
else
checkDeclined = true
})
return checkDeclined;
}));
您的代码中存在逻辑错误:一旦第一列的 headerName 不是 'Declined',它将始终 return 为真。
将 return true
块放在循环之外。
this.gridOptions.api.setRowData(this.arrayToFilter.filter(function(item) {
for(var i = 0; i < columns.length; i++) {
var temp = columns[i].headerName
if(item[temp] === 'Declined')
return false
}
return true
}));
我正在使用 filter
方法过滤出一个数组,如下所示:
this.gridOptions.api.setRowData(
this.arrayToFilter.filter(obj => obj.status !== "Declined")
);
上面的代码工作正常,但是当我尝试遍历 table 的所有列字段时它不起作用,这是我的代码:
this.gridOptions.api.setRowData(this.arrayToFilter.filter(function(item) {
for(var i = 0; i < columns.length; i++) {
var temp = columns[i].headerName
if(item[temp] === 'Declined')
return false
return true
}
}));
我在循环中做错了什么?
改用map迭代
this.gridOptions.api.setRowData(this.arrayToFilter.filter(function(item) {
let checkDeclined = true;
columns.map(column => {
let temp = columns[i].headerName
if(item[temp] === 'Declined')
checkDeclined = false
else
checkDeclined = true
})
return checkDeclined;
}));
您的代码中存在逻辑错误:一旦第一列的 headerName 不是 'Declined',它将始终 return 为真。
将 return true
块放在循环之外。
this.gridOptions.api.setRowData(this.arrayToFilter.filter(function(item) {
for(var i = 0; i < columns.length; i++) {
var temp = columns[i].headerName
if(item[temp] === 'Declined')
return false
}
return true
}));