Array.prototype.splice()

Deze vertaling is niet volledig. Help dit artikel te vertalen vanuit het Engels.

De splice() methode past de inhoud van een array aan door bestaande elementen te verwijderen en/of nieuwe elementen toe te voegen.

Syntax

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Parameters

start
Positie vanwaar de array dient te worden veranderd (met eerste element op 0). Indien groter dan de lengte van de array zal de start worden omgezet naar de lengte van de array. Indien negatief begint hij het absolute aantal vanaf het einde van de array.
deleteCount
Een getal dat aanduid hoeveel elementen moeten worden verwijderd. Indien 0 worden er geen elementen verwijderd. In dit scenario moet je minstens één toe te voegen element meegeven. Als de deleteCount groter is dan het overige aantal elementen in de array (beginnend bij de start waarde) worden alle deze overige elementen verwijderd.
Indien de deleteCount niet word meegegeven word deze alsvolgt berekend: (arr.length - start). Dit heeft als resultaat dat alle elementen na de startwaarde worden verwijderd.
item1, item2, ...
De elementen die in de array moeten worden toegevoegd, startende van de start waarde. Indien niet meegegeven zullen er enkel elementen uit de array verwijderd worden.

Return value

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

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.

Examples

Using splice()

The following script illustrates the use of splice():

var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];

// removes 0 elements from index 2, and inserts 'drum'
var removed = myFish.splice(2, 0, 'drum');
// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon']
// removed is [], no elements removed

// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon'] 
// removes 1 element from index 3
removed = myFish.splice(3, 1);
// myFish is ['angel', 'clown', 'drum', 'surgeon']
// removed is ['mandarin']

// myFish is ['angel', 'clown', 'drum', 'surgeon'] 
// removes 1 element from index 2, and inserts 'trumpet'
removed = myFish.splice(2, 1, 'trumpet');
// myFish is ['angel', 'clown', 'trumpet', 'surgeon']
// removed is ['drum']

// myFish is ['angel', 'clown', 'trumpet', 'surgeon'] 
// removes 2 elements from index 0, and inserts 'parrot', 'anemone' and 'blue'
removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue');
// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon']
// removed is ['angel', 'clown']

// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon'] 
// removes 2 elements from index 2
removed = myFish.splice(myFish.length -3, 2);
// myFish is ['parrot', 'anemone', 'surgeon']
// removed is ['blue', 'trumpet']

const myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon'];
// removes 3 elements starting at index 2
const removed = myFish.splice(2);
// myFish is ['parrot', 'anemone']
// removed is ['blue', 'trumpet', 'surgeon']

Specifications

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
The definition of 'Array.prototype.splice' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Array.prototype.splice' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'Array.prototype.splice' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 1.0 (1.7 or earlier) 5.5 (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

Backward compatibility

In JavaScript 1.2 the splice() method returns the element removed, if only one element is removed (deleteCount parameter is 1); otherwise, the method returns an array containing the removed elements.

Note: The last browser to use JavaScript 1.2 was Netscape Navigator 4, so you can depend on splice() always returning an array. This is the case, when a JavaScript object has a length property and a splice() method, console.log() treats it as an Array-like object. Checking with an instanceof Array on it returns false.

See also

  • push() / pop() — add/remove elements from the end of the array
  • unshift() / shift() — add/remove elements from the beginning of the array
  • concat() — returns a new array comprised of this array joined with other array(s) and/or value(s)

Documentlabels en -medewerkers

 Aan deze pagina hebben bijgedragen: QuintusOostendorp, yannickvergeylen
 Laatst bijgewerkt door: QuintusOostendorp,