0
deepSearchObject(obj) {
  let duplicateExists = false;

  Object.keys(obj).forEach(key => {
    if(key === 'fieldCode') {
      let value = obj[key]
      if(this.fieldCodes.includes(value)) {
        duplicateExists = true;
        return;
      } else {
        this.fieldCodes.push(value);
      }
    }
    if (typeof obj[key] === 'object') {
            this.deepSearchObject(obj[key])
    }
  })

  console.log('RETURN VALUE', duplicateExists);
  return duplicateExists;
},

Ideally I want to return from deepSearchObject when a duplicate is found, no more reason to traverse the object, currently instead of returning on the last line, the code above will log out the return value of True and then continue to execute, why? I've broken out of the loop with the return, do I need something else to break out of recursion?

Adam Weitzman
  • 1,552
  • 6
  • 23
  • 45
  • `return false;` to terminate the forEach, or throw an exception and put the logic in a try catch block. – Taplar Jun 05 '20 at 17:42
  • same behavior with return false – Adam Weitzman Jun 05 '20 at 17:45
  • Hmm, could have swore return false terminated forEach. Looks like that's incorrect. – Taplar Jun 05 '20 at 17:46
  • You cannot "break out of recursion". The most you can do is to `return` a result to the caller. Then, when the recursive call returns something, you *also* need to break from the loop. – Bergi Jun 05 '20 at 17:47
  • its correct it does, but the loop continues, is it running in a seperate recursive call on the call stack that hasn't been terminated? – Adam Weitzman Jun 05 '20 at 17:47
  • @Taplar `return false` terminates `jQuery.each` afaik. – Bergi Jun 05 '20 at 17:47
  • https://jsfiddle.net/vqnL9t6e/ @Bergi yeah, that's what i was remembering. – Taplar Jun 05 '20 at 17:47
  • 1
    From [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach): `There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.` – Matt Burland Jun 05 '20 at 17:47
  • That's because you can't break out of a `forEach` loop and `return` doesn't work either. I'd consider using `for ..loop` or `for..of..loop` for this – ABGR Jun 05 '20 at 17:48
  • guys this is not a for loop foreach loop issue – Adam Weitzman Jun 05 '20 at 17:48
  • in the code above even though i break out of the foreach it still keeps running as if the other recursions are finishing – Adam Weitzman Jun 05 '20 at 17:49
  • @AdamWeitzman - yes it is. `forEach` doesn't do what you expect it to do. Use a different loop construct that will do what you want it to do. – Matt Burland Jun 05 '20 at 17:49
  • @Bergi closed the question so i cant edit it, i added return false and the same behavior is happening – Adam Weitzman Jun 05 '20 at 17:51
  • Related to all that. Shouldn't the logic be `duplicateExists = this.deepSearchObject(obj[key])` since each call gets it's own version of the variable? – Taplar Jun 05 '20 at 17:51
  • ooo @Taplar i think you're onto it there – Adam Weitzman Jun 05 '20 at 17:52
  • 1
    @AdamWeitzman You still can [edit] the question. But what you really need to change is the `Object.keys().forEach` into a `for in`, just adding `return false` does not help - see the duplicate for more explanation of that issue. – Bergi Jun 05 '20 at 17:53
  • im not seeing the change but will keep looking – Adam Weitzman Jun 05 '20 at 18:02
  • changing to for in has the same behavior looks like – Adam Weitzman Jun 05 '20 at 18:06

0 Answers0