How to skip one iteration by using index number while doing a for loop on array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Idleman

If I have a array of nodes like this

var items = [obj01, obj02, obj03]

I like to know if there is a way to perform a for loop of items but skip obj02 by using the index number.

for example, if I put

if i == 2: 
  continue

the 2 is only recognised by value not the index order in the array, not sure if continue could work with index order instead of value. Thank you.

:bust_in_silhouette: Reply From: bloodsign

Hacky way:

var index = 0:
for n in array:
   if index != 2:
      pass #do something, if index is 2 skip it
   index+=1

or traditional while loop:

var index = 0
while(index<array.size()):
   if index !=2:
      pass #do something if index is 2 skip it
   index += 1
:bust_in_silhouette: Reply From: HaleyHalcyon

A couple different approaches.

# skip if i is 2
var items = [obj01, obj02, obj03]
for i in range(len(items)):
  if i == 2:
    continue
  else:
    do_thing(items[i])

Or:

# shift i one over at the right time
var items = [obj01, obj02, obj03]
for i in range(len(items) - 1):
  if i >= 2:
    i += 1
  do_thing(items[i])

Or even:

# make a "for loop" using a while loop
var items = [obj01, obj02, obj03]
var i = 0
while i < len(items):
  do_thing(items[i])
  if i == 1:
    i = 3
  else:
    i += 1

Thank you for your help.

Idleman | 2021-02-20 23:29