METHOD:  Array::splice


Array.splice(index, howMany, [element1][, ..., elementN])
 
The splice method is used to add and/or remove elements of an array returning an array of the elements removed. You first need to specify the index at which you wish to start removing elements, and the number to remove. (if 'howMany' is 0, you should specify at least 1 element to add). The following code creates an array 'cars' and then displays two elements starting with 'cars[1]':
 
Code:
cars = ["Mercedes", "Ford", "Chrysler", "Honda", "Volvo"]
document.write(cars.splice(1,2))

 
Output:
Ford,Chrysler
 
You can also include optional new elements to replace the ones removed. Expanding on the previous example, the following code would create an array consisting of the two extracted elements "Ford" and "Chrysler", but replace them with "Citreon" in the original array:
 
Code:
cars = ["Mercedes", "Ford", "Chrysler", "Honda", "Volvo"]
removed_cars = cars.splice(1, 2, "Citreon")
document.write(removed_cars + "<BR>")
document.write(cars)

 
Output:
Ford,Chrysler
Mercedes,Citreon,Honda,Volvo


Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information