Jump to content

[SOLVED] Arrays


Michdd

Recommended Posts

I've been looking for a solution to my problem of how to remove all elements from an array that have a certain value.. All over the internet I find examples like this:

 

function removeByElement(arrayName,arrayElement)
{
    for(var i=0; i<arrayName.length;i++ )
     { 
        if(arrayName[i]==arrayElement)
            arrayName.splice(i,1); 
      } 
  }

 

Which is basically what I created, except that won't work.. Because during the loop everytime you splice out an element arrayName.length gets smaller, so it doesn't allow you to get to all elements of the array..

 

Does anyone know a solution to this?..

Link to comment
Share on other sites

pull the array length out of the for() loop, and instead put in a static variable:

 

for_loop_length = arrayName.length;
    for(var i=0; i<for_loop_length;i++ )

I tried that, for some reason it's still only removing about half of the elements with that name.

Link to comment
Share on other sites

I also tried looping through creating an array of indexes to remove and then loop through that.

 

I'm pretty sure that my problem is: As the elements are removed their indexes are decreasing to numbers that have already been looped through.

 

Eg: I remove element with the index of 5, then index 6 gets moved down to 5, so I can't remove that.

Link to comment
Share on other sites

I also tried looping through creating an array of indexes to remove and then loop through that.

 

I'm pretty sure that my problem is: As the elements are removed their indexes are decreasing to numbers that have already been looped through.

 

Eg: I remove element with the index of 5, then index 6 gets moved down to 5, so I can't remove that.

 

have you tested whether this is the case? if it is, then technically after you use splice() that same index should echo a different value:

 

for (stuff)
{
  alert('pre-splice index is '+i+' with a value of '+arrayName[i]);
  arrayName.splice(i,1);
  alert('post-splice index is '+i+' with a value of '+arrayName[i]);
}

 

EDIT: if this is the case, then one way around it is to set that value to null rather than splicing it out, and ignore any null values in all future loops. alternatively, you could try the following syntax for the for() loop:

 

for (var i in arrayName)

 

not sure if that will alleviate your issue if you continue to use splice() though.

Link to comment
Share on other sites

function arr_remove( haystack, needle ) {

  // terribly inefficient :(

  var new_arr = [], t;

  while( haystack.length > 0 ) {

    t = haystack.shift(); // or unshift, whichever takes off the first element

    if( t !== needle ) {

      new_arr.push( t );

    }

  }

  return new_arr;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.