Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1308 How to you remove an element or item from an array (ie make it smaller)?

You are here: irt.org | FAQ | JavaScript | Object | Q1308 [ previous next ]

You can remove an element by looping through the array and removing the last item:

<script language="JavaScript"><!--
function RemoveElement(elementnr) {
    // elementnr is the number of the element you wish to remove
    // the name of the array used here is array

    for (elementnr<array.length,elementnr++) {
        // assigns the value of elementnr+1 to elementnr, so you move all items by 1
        array[elementnr] = array[elementnr + 1];
    }
    array.length=array.length-1;
    // Yes it is as easy as this!
}
//--></script>

Submitted by Erik Versaevel

©2018 Martin Webb