Array.prototype.splice()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/splice
  • Revision title: Array splice method
  • Revision id: 26367
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 1 words added, 14 words removed

Revision Content

Summary

Changes the content of an array, adding new elements while removing old elements.

Method of Array
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition

Syntax

array.splice(index, howMany[, element1[, ...[, elementN]]])

Parameters

index 
Index at which to start changing the array. If negative, will begin that many elements from the end.
howMany 
An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.
element1, ..., elementN 
The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array.

Description

If you specify a different number of elements to insert than the number you're removing, the array will have a different length at the end of the call.

The splice method returns an array containing the removed elements. If only one element is removed, an array of one element is returned.

Backward Compatibility

JavaScript 1.2

The splice method returns the element removed, if only one element is removed (howMany parameter is 1); otherwise, the method returns an array containing the removed elements. Note that the last browser to use JavaScript 1.2 was Netscape Navigator 4, so you can depend on splice always returning an array.

Examples

Example: Using splice

The following script illustrate the use of splice:

// assumes a print function is defined
var myFish = ["angel", "clown", "mandarin", "surgeon"];
print("myFish: " + myFish);

var removed = myFish.splice(2, 0, "drum");
print("After adding 1: " + myFish);
print("removed is: " + removed);

removed = myFish.splice(3, 1);
print("After removing 1: " + myFish);
print("removed is: " + removed);

removed = myFish.splice(2, 1, "trumpet");
print("After replacing 1: " + myFish);
print("removed is: " + removed);

removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
print("After replacing 2: " + myFish);
print("removed is: " + removed);

This script displays:

myFish: angel,clown,mandarin,surgeon
After adding 1: angel,clown,drum,mandarin,surgeon
removed is: 
After removing 1: angel,clown,drum,surgeon
removed is: mandarin
After replacing 1: angel,clown,trumpet,surgeon
removed is: drum
After replacing 2: parrot,anemone,blue,trumpet,surgeon
removed is: angel,clown

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/splice", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Array/splice", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array/splice" } ) }}

Revision Source

<h2>Summary</h2>
<p>Changes the content of an array, adding new elements while removing old elements.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Array" title="en/JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.2</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 3rd Edition</td> </tr> </tbody>
</table><h2>Syntax</h2>
<p><code><em>array</em>.splice(<em>index</em>, <em>howMany</em>[, <em>element1</em>[, ...[, <em>elementN</em>]]])</code></p>
<h2>Parameters</h2>
<dl> <dt><code>index</code> </dt> <dd>Index at which to start changing the array. If negative, will begin that many elements from the end.</dd>
</dl>
<dl> <dt><code>howMany</code> </dt> <dd>An integer indicating the number of old array elements to remove. If <code>howMany</code> is 0, no elements are removed. In this case, you should specify at least one new element. If no <code>howMany</code> parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after <code>index</code> are removed.</dd>
</dl>
<dl> <dt><code>element1, ..., element<em>N</em></code> </dt> <dd>The elements to add to the array. If you don't specify any elements, <code>splice</code> simply removes elements from the array.</dd>
</dl>
<h2>Description</h2>
<p>If you specify a different number of elements to insert than the number you're removing, the array will have a different length at the end of the call.</p>
<p>The <code>splice</code> method returns an array containing the removed elements. If only one element is removed, an array of one element is returned.</p>
<h2>Backward Compatibility</h2>
<h3>JavaScript 1.2</h3>
<p>The <code>splice</code> method returns the element removed, if only one element is removed (<code>howMany</code> parameter is 1); otherwise, the method returns an array containing the removed elements. Note that the last browser to use JavaScript 1.2 was Netscape Navigator 4, so you can depend on <code>splice</code> always returning an array.</p>
<h2>Examples</h2>
<h3>Example: Using <code>splice</code></h3>
<p>The following script illustrate the use of splice:</p>
<pre class="brush: js">// assumes a print function is defined
var myFish = ["angel", "clown", "mandarin", "surgeon"];
print("myFish: " + myFish);

var removed = myFish.splice(2, 0, "drum");
print("After adding 1: " + myFish);
print("removed is: " + removed);

removed = myFish.splice(3, 1);
print("After removing 1: " + myFish);
print("removed is: " + removed);

removed = myFish.splice(2, 1, "trumpet");
print("After replacing 1: " + myFish);
print("removed is: " + removed);

removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
print("After replacing 2: " + myFish);
print("removed is: " + removed);
</pre>
<p>This script displays:</p>
<pre class="eval">myFish: angel,clown,mandarin,surgeon
After adding 1: angel,clown,drum,mandarin,surgeon
removed is: 
After removing 1: angel,clown,drum,surgeon
removed is: mandarin
After replacing 1: angel,clown,trumpet,surgeon
removed is: drum
After replacing 2: parrot,anemone,blue,trumpet,surgeon
removed is: angel,clown
</pre>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/splice", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Array/splice", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array/splice" } ) }}</p>
Revert to this revision