mercredi 1 avril 2020

Stopping loop when condition is met

I would like to loop through the whole array, and stop when my if-condition is met, then put the value in my var checked.

If the if-condition is not met, the var should be set false.

When I put the break my script outputs false, which it shouldn't since 2002 exists in the array.

subscriber = [2001, 2002, 2004]

for(i = 0; i < subscriber.length; i++) {
  if (subscriber[i].ID == '2002') {
    checked = 'true';
    break; //added break here
  } else {
    checked = 'false';
  }
  Write(checked);
}

(please don't mind the properties of the object e.g. ID)

Aucun commentaire:

Enregistrer un commentaire