Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Object | Q1308 [ previous next ]

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

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


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.