Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip an iteration in a for in loop JavaScript? [duplicate]

Tags:

javascript

I want to able to skip an iteration in this for in break is just stopping it..

   for (const property in data) {
              if (property === 'user' && !context.user) {
                break;
              }
              localStorage.setItem(property, data[property]);
          
            }

How to skip an iteration if certain condition is met in a for loop

like image 660
Richardson Avatar asked Mar 08 '26 07:03

Richardson


2 Answers

You are using the break, it will exit the loop. Using continue to skip current looping, and jump to next round.

for (const property in data) {
    if (property === 'user' && !context.user) {
        continue;
    }
    localStorage.setItem(property, data[property]);
}
like image 161
Allen Chak Avatar answered Mar 09 '26 19:03

Allen Chak


You should use continue instead of break

OR

for (const property in data) {
  if (property !== "user" || context.user) {
    localStorage.setItem(property, data[property]);
  }
}
like image 30
Rahul Sharma Avatar answered Mar 09 '26 19:03

Rahul Sharma