A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Remove element from array

  1. #1
    Junior Member
    Join Date
    Mar 2008
    Posts
    23

    Question 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

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    splice is the way to go to remove a specific index....pop always removes the last index in the array.
    ~calmchess~

  3. #3
    Junior Member
    Join Date
    Mar 2008
    Posts
    23
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center