Core JavaScript 1.5 Reference:Global Objects:Array:slice
From MDC
Contents |
[edit] Summary
Extracts a section of an array and returns a new array.
| Method of Array | |
| Implemented in: | JavaScript 1.2, NES 3.0 |
| ECMA Version: | ECMA-262 Edition 3 |
[edit] Syntax
slice(begin[,end])
[edit] Parameters
-
begin - Zero-based index at which to begin extraction.
- As a negative index,
startindicates an offset from the end of the sequence.slice(-2)extracts the second-to-last element and the last element in the sequence.
-
end - Zero-based index at which to end extraction.
sliceextracts up to but not includingend. -
slice(1,4)extracts the second element through the fourth element (elements indexed 1, 2, and 3). - As a negative index,
endindicates an offset from the end of the sequence.slice(2,-1)extracts the third element through the second-to-last element in the sequence. - If
endis omitted,sliceextracts to the end of the sequence.
[edit] Description
slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:
- For object references (and not the actual object),
slicecopies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
- For strings and numbers (not String and Number objects),
slicecopies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.
If a new element is added to either array, the other array is not affected.
[edit] Examples
[edit] Example: Using slice
In the following example, slice creates a new array, newCar, from myCar. Both include a reference to the object myHonda. When the color of myHonda is changed to purple, both arrays reflect the change.
// Using slice, create newCar from myCar.
var myHonda = { color: "red", wheels: 4, engine: { cylinders: 4, size: 2.2 } };
var myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
var newCar = myCar.slice(0, 2);
// Print the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
print("myCar = " + myCar.toSource());
print("newCar = " + newCar.toSource());
print("myCar[0].color = " + myCar[0].color);
print("newCar[0].color = " + newCar[0].color);
// Change the color of myHonda.
myHonda.color = "purple";
print("The new color of my Honda is " + myHonda.color);
// Print the color of myHonda referenced from both arrays.
print("myCar[0].color = " + myCar[0].color);
print("newCar[0].color = " + newCar[0].color);
This script writes:
myCar = [{color:"red", wheels:4, engine:{cylinders:4, size:2.2}}, 2, "cherry condition", "purchased 1997"]
newCar = [{color:"red", wheels:4, engine:{cylinders:4, size:2.2}}, 2]
myCar[0].color = red
newCar[0].color = red
The new color of my Honda is purple
myCar[0].color = purple
newCar[0].color = purple