Home > Net >  Jacascript Loop - Skip A Step based on a variable
Jacascript Loop - Skip A Step based on a variable

Time:03-30

I have a For loop with steps where I need to be able to skip a step if a value in a variable equals a certain text.

For example - in the below code I would like to skip step 0 if that var airport does not equal JFK:

var airport = 'JFK';

for (let step = 0; step < 2; step  ) {
    rec.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        value: 1261,
        forceSyncSourcing: true
    })
)

CodePudding user response:

You want to use a conditional statement with continue to terminate that particular iteration if the condition is met.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue

Simple working snippet below. Note loop 0 is missed.

var airport = 'JFK';

for (let step = 0; step < 4; step  ) {
   if (airport == 'JFK' && step==0) {continue}
    // code;
    // code;
    
    console.log(`step ${step} executed`);
}

console.log("loop finished");

  • Related