...

View Full Version : problem emptying array (1 by 1)



deadstone
04-26-2004, 03:02 PM
Hi,

I have a JS Array which I add elements to which relate to rows in a table on my web page.

When the users delete records from the db, I want to empty the array.

I have tried using the splice method to remove that array element each time, and I have also tried declaring a new array...

but the array still keeps the elements inside...

>>>> CODE >>>>
iMaxRows = oCheckedRows.length
for (iIndx=iMaxRows-1; iIndx>=0; iIndx--)
{
sArrVal=oCheckedRows[iIndx]
oRow=oTable.rows('tr' + sArrVal)
alert('delete row: ' + oRow.rowIndex)
oTable.deleteRow(oRow.rowIndex)
oCheckedRows.splice(iIndx, 1)
}
//oCheckedRows = new Array()
<<<< END CODE <<<<

I am looping backwards thru the array from the highest index to th zero one, splicing eachg time. Surely this should give me an empty array again?
and if not, how can I redeclare it properly and point at the same place in memory...?

what am I doing wrong?

thanks

Philip

jbot
04-26-2004, 03:14 PM
shouldn't you be using pop() instead. this is specifically designed for removing the last element in an array.

deadstone
04-26-2004, 03:32 PM
Hi,

that doesn't work either... it's very strange...

if I check the length of the array after the loop, it is zero...but when I click on the Edit button the script still thinks there are elements in the array...

liorean
04-26-2004, 07:45 PM
It would seem you haven't given us all the code that we need to understand your problem. What does the button do, for instance?


As for the code, why don't you go
var
iMaxRows=oCheckedRows.length;
while(iMaxRows-->0){
sArrVal=oCheckedRows.pop();
oRow=oTable.rows('tr' + sArrVal);
alert('delete row: ' + oRow.rowIndex);
oTable.deleteRow(oRow.rowIndex);
}

shlagish
04-26-2004, 10:22 PM
Emptying an array..


for(1=0;i<yourArray.length;++i){
yourArray[i]="";
}

or if they are numbers:
yourArray[i]=0;

Wouldn't that work?

liorean
04-27-2004, 02:01 AM
Well, the fastest way of emptying an array is to simply set the [object Array].length property to 0. Then it will be automatically truncated. (I used this approach in my array mending script, for instance.)

However, he needed to use the value from each of the items, and that's when the [object Array].pop method is really useful.

glenngv
04-27-2004, 03:26 AM
Be sure to prototype (http://www.codingforums.com/showthread.php?t=19996&highlight=Array.prototype.pop) Array.pop for IE5 support. That script came from liorean.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum