Deleting multiple array elements by indexes

Hello Yjs community,

The following code will sometimes result in × Unhandled Rejection (Error): Length exceeded!, depending on how many and where deletions are happening.

const elementArray: Y.Array<Y.Map<any>> = doc.getArray("myarray");

doc.transact(() => {
  elementArray.forEach((element: Y.Map<any>, index: number) => {
    if (...) { // Some arbitrary condition.
      elementArray.delete(index);
    }
  });
});

I stepped through the code and noticed the delete() call immediately removes the element and reduces the array length by 1, unsurprisingly. Meaning deleting an element at index is impossible when length - deleted < index. So my solution is to add a counter like this:

const elementArray: Y.Array<Y.Map<any>> = doc.getArray("myarray");

doc.transact(() => {

  let deletedCount = 0;

  elementArray.forEach((element: Y.Map<any>, index: number) => {
    if (...) { // Some arbitrary condition.
      elementArray.delete(index - deletedCount);
      deletedCount++;
    }
  });
});

This works but feels problematic for reasons unknown. I haven’t tested this in a real-world collaboration environment either. So my question is: is there a proper way to delete multiple elements from an array with non-sequential indexes? Am I missing something?

Thank you!

Joe