Nested Whiles

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

JoshuaW
Ebase User
Posts: 19
Joined: Fri Oct 24, 2008 1:55 pm
Location: WFH, Oxfordshire, UK
Contact:

Nested Whiles

#1

Postby JoshuaW » Thu Nov 24, 2022 3:56 pm

Hi all,

I would like to share this, whilst putting this together using nested whiles.
  • 1st Table are list of categories
  • 2nd Tables are customer's assigned categories
Incorrect way

Code: Select all

var cats = table.CATEGORIES.rows;
var owns = tables.OWN_CATEGORIES.rows;
while(cats.next()){
  while(owns.next()){
     if(tables.CATEGORIES.CAT_ID.value == tables.OWN_CATEGORIES.CAT_ID.value){
       log('loop2: ' + tables.OWN_CATEGORIES.CAT_ID.value);
       break;
     }
  }
}
Suggested method using nested whiles

Code: Select all

var cats = table.CATEGORIES.rows;
while(cats.next()){
  var owns = tables.OWN_CATEGORIES.rows;
  while(owns.next()){
     if(tables.CATEGORIES.CAT_ID.value == tables.OWN_CATEGORIES.CAT_ID.value){
       log('loop2: ' + tables.OWN_CATEGORIES.CAT_ID.value);
       break;
     }
  }
}
Although if any CATEGORIES have duplicated rows for different purposes, then a while break is not recommend.
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

Re: Nested Whiles

#2

Postby Steve » Thu Feb 16, 2023 12:02 pm

Hi Joshua,

I would tweak so that you break out of the outer (cats) loop too.

Code: Select all

var cats = table.CATEGORIES.rows;
var match = false;
while(cats.next()){
  var owns = tables.OWN_CATEGORIES.rows;
  while(owns.next()){
     if(tables.CATEGORIES.CAT_ID.value == tables.OWN_CATEGORIES.CAT_ID.value){
       log('loop2: ' + tables.OWN_CATEGORIES.CAT_ID.value);
       match = true;
       break;
     }
  }
  if(match)
    break;
}
Otherwise you will iterate through all the tables rows again on the next iteration of cats.

Thanks

Steve
0 x


Who is online

Users browsing this forum: No registered users and 77 guests