# Multiple object intersection and removal

**URL:** <https://discourse.processing.org/t/multiple-object-intersection-and-removal/22900>\
**Category:** Coding Questions\
**Created:** [July 29, 2020, 9:10am UTC](https://discourse.processing.org/t/multiple-object-intersection-and-removal/22900 "2020-07-29T09:10:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 29, 2020, 11:30am UTC](https://discourse.processing.org/t/multiple-object-intersection-and-removal/22900/2 "2020-07-29T11:30:08Z")

</div>

> [@AbrarShahriar](#):
>
> I kinda did it but it only works for a couple of seconds. Then the code just breaks.

Backwards loops are a great way to iterate & remove an array’s current item at the same time.

However you attempt to **splice()** the indices _j_ & _i_ from the same array at 1 swoop.

That makes all items inside that array to left-shift their indices 2 times while the outer loop is still dealing w/ its index _i_.

In short, we can’t use method **splice()** to delete more than 1 index from the same array on 1 single iteration.

As a workaround, rather than mutating the array inside that double loop, we merely collect on a separate array the indices marked to be removed later.

- Let’s call that extra array _toRemoveIndices[]_.
- Before starting the double loop, we clear that array like this: `toRemoveIndices.length = 0;`
- Now, inside the inner loop, we replace the **splice()** calling w/ `toRemoveIndices.push(i, j);`
- Once the double loop is finished we can finally use **splice()** in order to delete each _animat[]_'s index matching the indices collected by _toRemoveIndices[]_.
- We can even create a function for that task. I’ve just named it as **removeIndices()**.

The excerpt below are the changes I’ve made to your posted code:

```auto
const animats = [], toRemoveIndices = [];

function draw() {
  background(255, 100, 100);

  var j, i = animats.length;
  toRemoveIndices.length = 0;

  while (i--) {
    const a = animats[j = i];

    a.birth();
    a.grow();

    while (j--) if (a.intersects(animats[j])) {
      toRemoveIndices.push(i, j);
      break;
    }
  }

  removeIndices(animats, toRemoveIndices);
}

function removeIndices(array, indices) {
  for (const index of indices) array.splice(index, 1);
  return array;
}

```

---

_[View the full topic](https://discourse.processing.org/t/multiple-object-intersection-and-removal/22900)._
