0

how to conditionally check &return true from forEach function for an example like below? Thanks!

this.results.forEach(el => {
  el.forEach(el => {
    if (el == 3) {
      //return true here is not working  
    }
  })
});

if (this.SlantLeft == 3 || this.SlantRight == 3) {
  return true //like this
}
ambianBeing
  • 3,449
  • 2
  • 14
  • 25
  • do you have some data to test and the wanted result? – Nina Scholz Sep 03 '19 at 17:14
  • 2
    You can't, forEach ignores the return value of its callbacks. Maybe you want .find instead? – jonrsharpe Sep 03 '19 at 17:15
  • Use [`for...of`](https://stackoverflow.com/a/32101207/3082296) with return or use [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – adiga Sep 03 '19 at 17:16
  • Okay thanks it's fine but when I'm working on one array, how to do this when i have arrays in array? – Konrad Ryczko Sep 03 '19 at 17:24
  • You add nested `for...of` like you have nested forEach. `for (let el of this.results) for (let n of el) if(n === 3) return true` (You have named both the forEach callback's parameter as `el`) – adiga Sep 03 '19 at 17:28

1 Answers1

0

I think you want some(), which will return true if any of the iterable elements meets a condition, otherwise false.

this.results.some(el => el == 3);
pwilcox
  • 5,542
  • 1
  • 19
  • 31