gpt4 book ai didi

javascript - 预期在箭头函数末尾返回一个值。 (一致返回)

转载 作者:行者123 更新时间:2023-12-01 01:43:40 25 4
gpt4 key购买 nike

在我的代码中,我想迭代一个对象数组,只要这些对象之一包含元素就返回 true,否则在循环结束时返回 false。看起来我的代码可以工作,但 ESLint 显示错误 [eslint] Expected to return a value at the end of arrow function。 (一致返回) 但如果条件为 false,我不想返回 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));
}

关于javascript - 预期在箭头函数末尾返回一个值。 (一致返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52143116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com