Amir-Mousavi
Amir-Mousavi

Reputation: 4593

Expected to return a value at the end of arrow function. (consistent-return)

in my code, I want to iterate over an array of objects, as soon as one of these objects contains the element return true otherwise at the end of loop return false. it seems that my code works but ESLint shows error [eslint] Expected to return a value at the end of arrow function. (consistent-return) but I don't want to return for instance false if the condition is false.

so my array looks like below. and the function afterward.

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;
  }

Upvotes: 0

Views: 2554

Answers (1)

Andrew Shepherd
Andrew Shepherd

Reputation: 45272

It appears that you want to determine if the statements is true for at least one item.

You should use the some function.

This stops looking after it has found the first match.

isOccupied(row, col) {
   const { myArray} = state[id];
   return myArray.some(key => key.location.includes(row+col));
}

Upvotes: 2

Related Questions