Michdd Posted August 31, 2009 Share Posted August 31, 2009 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?.. Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 31, 2009 Share Posted August 31, 2009 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++ ) Quote Link to comment Share on other sites More sharing options...
Michdd Posted August 31, 2009 Author Share Posted August 31, 2009 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. Quote Link to comment Share on other sites More sharing options...
Michdd Posted August 31, 2009 Author Share Posted August 31, 2009 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. Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 31, 2009 Share Posted August 31, 2009 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 31, 2009 Share Posted August 31, 2009 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.