Splice elements in an array
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ai_ya
Posted On:   Monday, February 9, 2004 07:51 PM

var string_array = new Array(); for(var i=0;i { string_array[i] = new Array(); } string_array[0] = new Array("cake","cheese","strawberry","cheese","butter","chocolate","cheese"); string_array[1] = new Array("cheese","cheese","cheese"); alert(string_array) var text = "cheese"; for(i=0;i { for(var j=0;j { if(string_array[i][j] == text) { var rem = string_array[i].splice(j,1); } } } alert(string_array) ]]>]]> Dear all, I have a script written as follows, I would like to remove all the matched elements when compare with text = "cheese", however i still get one element not removed from the array, can anyone point out my mistake? Btw, I m using javascript. Any kind help is greatly appreciated.    More>>

Dear all,

I have a script written as follows, I would like to remove all the matched elements when compare with text = "cheese", however i still get one element not removed from the array, can anyone point out my mistake? Btw, I m using javascript. Any kind help is greatly appreciated.

			








   <<Less

Re: Splice elements in an array

Posted By:   TheRam_s  
Posted On:   Tuesday, February 10, 2004 01:10 PM

The problem is when you delete, you should roll back. Else you will skip that element. So if you have an array with just "cheese" in it, only alternate elements will be deleted!!


if(string_array[i][j] == text)

{

var rem = string_array[i].splice(j,1);

j--;

}




hope this helps
About | Sitemap | Contact