-
Remove element from array
Hi !
I have an array used to track shots fired from a cannon, and when they hit the ground they are removed.
I would like to know what is the most efficient way of finding and removing a certain element from an array. I heard that using pop() instead of splice() was faster, is it ?
Also, the big question I have is that the only way I see of actually finding the element in the array is using
Code:
for(i=0;i<array.length;i++){
if(array[i] == movieClipToRemove){
//i == movieClip index in the array
}
}
Then I would use either splice() or pop() to remove it from the array before deleting the MC. Is there a better way than scanning through the whole array comparing each element to the triggering MC ?
Thank you guys
-
Senior Member
splice is the way to go to remove a specific index....pop always removes the last index in the array.
-
But you can use pop to remove specific element by using
array[index] = array.pop()
And it might just be faster considering it doesn't have to re-sort the whole array since only two elements are being changed. So I want to know which is faster, and also how to find that element in the array.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|