Array.splice() : Splice « Array « JavaScript Tutorial






Syntax

array.splice(start,delete,arg3,...,argN)

start -- The position in the array where the slice is to begin.

delete -- The number of elements to be deleted from the array, beginning at the position specified by start.

arg3,...,argN -- New array elements to be inserted into the array, starting at the position specified by start.

Returns -- If any elements are deleted from the array, they are returned from the method as an array.

The splice() method can either add elements to or delete elements from an array.

When the delete parameter contains a number other than zero, the elements beginning at start and ending at index start+ending are deleted from the array.

If delete is zero, no elements are deleted.

All elements from start to the end of the array are deleted when delete is not specified.

If arguments follow the delete parameter, they are added to the array as elements beginning at the position specified by start.

Existing elements are shifted up to allow room for the new elements.

<html>
    <script language="JavaScript">
    <!--
    function printOrder(theArray)
    {
      document.write("The current order is:<br>");
      for(i=0; i<theArray.length; i++)
      {
        document.write("- ",theArray[i],"<br>");
      }
    }

    myArray = new Array("A","B","C");
    document.write("<h3>The initial order taken</h3>");

    printOrder(myArray);

    myArray.splice(0,1,"D");
    printOrder(myArray);
    -->
    </script>
    </html>








11.30.Splice
11.30.1.Array.splice()
11.30.2.Using the splice() method of the Array object
11.30.3.Using the splice() method to replace elements
11.30.4.Using the splice() method to insert elements
11.30.5.Using splice method from zArray Library
11.30.6.Using splice method from zArray Library 2