在我的代码中,我想遍历一个对象数组,只要其中一个对象包含元素返回 true,否则在循环结束时返回 false。 似乎我的代码有效,但 ESLint 显示错误[eslint] Expected to return a value at the end of arrow function. (consistent-return)
但我不想返回例如false
条件是否为假。
所以我的数组如下所示。 以及之后的功能。
myArray:[{location:["rowcol","rowcol",...]},{location:[]},{location:[]},...]
isOccupied(row, col) {
const { myArray} = state[id];
myArray.forEach(key => { // here on arrow I see the error
if(key.location.includes(row+ col)) return true;
});
return false;
}
您似乎想要确定至少一个项目的语句是否为真。
您应该使用 some 函数。
这会在找到第一个匹配项后停止查找。
isOccupied(row, col) {
const { myArray} = state[id];
return myArray.some(key => key.location.includes(row+col));
}