预计在箭头函数结束时返回一个值。 (一致的回报)

问题描述 投票:0回答:1

在我的代码中,我想迭代一个对象数组,只要其中一个对象包含该元素返回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;
  }
javascript reactjs eslint
1个回答
2
投票

看来你想确定这些陈述是否适用于至少一个项目。

你应该使用some函数。

这停止了​​它找到第一场比赛后的照顾。

isOccupied(row, col) {
   const { myArray} = state[id];
   return myArray.some(key => key.location.includes(row+col));
}
© www.soinside.com 2019 - 2024. All rights reserved.