HTML and CSS Reference
In-Depth Information
FORMAT
var newArray = Arrayname.slice(first element, last element);
EXAMPLE
var ArraySlice = myArray.slice(2,6); // ArraySlice contains elements
// 2 through 5 of myArray.
EXAMPLE 9.14
<html>
<head><title>Array slice() method</title></head>
<body>
<script type="text/javascript">
1
var names=new Array("Dan", "Liz", "Jody", "Christian",
"William");
document.write("<b>Original array: "+ names + "<br />");
2
var sliceArray=names.slice(2, 4);
document.write("New array after the slice: ");
3
document.write(sliceArray);
</script>
</body>
</html>
EXPLANATION
1
This is the original array of names.
2
The slice() method will start at position 2, copy Jody into the new array, then
Christian, and stop just before position 4, William. The original array is not affect-
ed by the slice.
3
The new array created by the slice() method is displayed (see Figure 9.17).
Figure 9.17 Using Array slice() to copy elements of an array.
The splice() Method. The splice() method removes a specified number of ele-
ments from some starting position in an array and allows you to replace those items
 
Search WWH ::




Custom Search