Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove element [0] from an array

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi everyone,

I am using the following to remove items from an array i am building up. Users add items to the array and may need to remove them too, it all works ok, except when we get to the last element.

Code:
function removeLine(id){
    if(id == ''){
        return;
    }
    this.lines.splice(id,1);
    }

this is part of an object, hence the "this.lines" to identify the array.

It appears to simply fail when you effectively call
Code:
    this.lines.splice(0,1);

I can see why this might be the right thing if there is only one element in the array, but it causes problems when there are several items in the array and it is the first that needs to be removed.

Any suggestions?

Thanks.
 
update....

now i am trying this ;-

Code:
function removeLine(id){
    if(id == ''){
        return;
    }
    if(id == 0){
        this.lines.shift();
    }else{
        this.lines.splice(id,1);
        }
}

but it is still not removing the item.
My error panel is clean so i am a bit lost on what its doing.
 
[SOLVED]

Code:
if(id == ''){
        return;
    }


removed that and it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top