How to break/continue across nested for each loops in Type Script

forEach accepts a function and runs it for every element in the array. You can't break the loop. If you want to exit from a single run of the function, you use return.

If you want to be able to break the loop, you have to use for..of loop:

  for(let name of group.names){
    if (name == 'SAM') {
      break;
    }
  }

ForEach doesn't support break, you should use return

  groups =[object-A,object-B,object-C]
        groups.forEach(function (group) {
        // names also an array
            group.names.forEach(function (name) {

            if (name == 'SAM'){
             return; //
          }
     }
   }

Object.keys(fields).forEach(function (key, index) {
  if (fields[key] !== null && fields[key].toString().trim().length === 0) {
    console.log('error');
    return;
  }
});

Tags:

Typescript