How to delete some elements correctly when deleting an array loop?

Iterate backwards to destroy and remove indexes.

If you go into a loop macro you will see this warning:

The point is that after deleting the current element of the iteration, the next element of the array will be skipped.
You need to either create your own macro that will have an Exec pin to delete the current element, which will do it back to one index so as not to miss the next element, or create a buffer that will contain the elements that need to be deleted, and then you will delete them in the next loop.

Thank you for your question and answer, is there a complete sample code?

What you’re doing in the image looks fine, are you having any issues ?

Duplicate the For Each Loop macro and add the selected nodes to it.

After removing an element, Loop Body will be called again at the same index, but in its place there will be a new element that has moved after the removal.

In my case, the Array Index during execution will be 0 1 1.

If its any help, something like:

To correctly delete elements in an array loop:

  1. Iterate backwards to avoid index shifting.

  2. Use splice() for arrays or delete for objects.

  3. Consider filter() for a new array without deleted elements.

  4. Use a while loop with a condition to remove multiple elements.

  5. Be cautious with forEach(), as it can lead to unexpected behavior.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.