0

I used this line of code to remove an element:

tiles.splice(tiles.indexOf(tiles[i]), 1);

Yet afterwards when I'm checking the value, it's still not null, in fact, it still contains the movieclip it had inside of it.

This worked though:

tiles[tr] = null;

The question is, is it still okay to do it like that? I have movieclips added and removed to this array and I type removeChild(tiles[tr); before removing it from the array.

I just don't want to encounter some terrible performance in the future,

Thanks.

3
  • 1
    tiles.splice(i, 1) will work, no need to use indexOf, see if this resolves the issue. Also are you wanting to have the contents of the element (a movieclip for example) tagged for garbage collection or just to have it removed from the array? Commented Mar 6, 2013 at 16:55
  • @Sim Just want it to be removed from the array, I won't need it any longer Commented Mar 6, 2013 at 17:18
  • Well splice(..) removes the element then shifts the remaining elements up one position, so if you splice the 2nd element in the array, the 3rd element will move up to take the position of the 2nd, the 4th will move to the 3rd position etc. This is maybe why your trace is still saying there is something at that position in the array. Commented Mar 6, 2013 at 17:23

1 Answer 1

1

splice cuts out the element from the Array, and all the above elements move one stept down. Which also reflects in the Array's length. So the element you are finding is the next element. Just nulling it will do just that. Leave the rest of the Array as is with an empty position.

Also you dont need to call indexOf if i is already the position you need.

tiles.splice(i, 1)

will do.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible not to move all of the above elements one step down?
Splice does exactly that: if you splice "2" from the array [1, 2, 3, 4] the array becomes [1, 3, 4]. If you don't splice and just null the element it becomes [1, null, 3, 4].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.