myArray.splice( start, deleteCount, [ newItem1 ], [ newItem2 ], [ ... ] ) in ECMAScript 262
Remove a section from the array and return it; optionally inserting new values in that place.
Arguments
name | type | description |
---|---|---|
start | Number | The index to start deleting fr |
deleteCount | Number | The number of items to remove. |
newItem1 | Object | [optional] The first item to insert. |
newItem2 | Object | [optional] The second item to insert. |
... | Object | [optional] etc. |
- Return Type
- Array
Description
A new array is created, and deleteCount
elements are copied from the start
index into the new array. They are also removed from the original array (modifying it).
If any additional parameters are present, they are inserted in that location. If any of those parameters are arrays, they are added as a single element. (The array is not traversed to insert each of its elements separately.)
As shown in the example below, this method may be used to insert items into an array at a particular location without removing any.
var peopleInLine = [ 'Dan' , 'Susan' , 'Emily' ];
//Susan lets someone into the line in front of her!
peopleInLine.splice(1,0,'Mr. Line Cutter');
// ** peopleInLine is now ['Dan','Mr. Line Cutter','Susan','Emily']
//Susan and her friend get kicked out of line and replaced by the elderly.
peopleInLine.splice(1,2,'Nice Old Lady','Nice Old Man');
// ** peopleInLine is now ['Dan','Nice Old Lady','Nice Old Man','Emily']
//Another line opens, and the first three people in the line go to it
var newLine = peopleInLine.splice(0,3);
// ** newLine is now ['Dan','Nice Old Lady','Nice Old Man']
// ** peopleInLine is now ['Emily']