Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2003
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem emptying array (1 by 1)

    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

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    shouldn't you be using pop() instead. this is specifically designed for removing the last element in an array.

  • #3
    New Coder
    Join Date
    Aug 2003
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RE: JS Array (pop)

    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...

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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
    Code:
    var
        iMaxRows=oCheckedRows.length;
    while(iMaxRows-->0){
        sArrVal=oCheckedRows.pop();
        oRow=oTable.rows('tr' + sArrVal);
        alert('delete row: ' + oRow.rowIndex);
        oTable.deleteRow(oRow.rowIndex);
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Emptying an array..
    Code:
    for(1=0;i<yourArray.length;++i){
     yourArray[i]="";
    }
    or if they are numbers:
    yourArray[i]=0;

    Wouldn't that work?
    Shawn

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Be sure to prototype Array.pop for IE5 support. That script came from liorean.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •