slice()
method returns a shallow copy of a portion of an array into a new array object selected from
start
to
end
(
end
not included) where
start
and
end
represent the index of items in that array. The original array will not be modified.
arr.slice([start[, end]])
start
可选
Zero-based index at which to start extraction.
slice(-2)
extracts the last two elements in the sequence.
start
is undefined,
slice
starts from the index
0
.
start
is greater than the index range of the sequence, an empty array is returned.
end
可选
slice
extracts up to but not including
end
。例如,
slice(1,4)
extracts the second element through the fourth element (elements indexed 1, 2, and 3).
slice(2,-1)
extracts the third element through the second-to-last element in the sequence.
end
被省略,
slice
extracts through the end of the sequence (
arr
.length
).
end
is greater than the length of the sequence,
slice
extracts through to the end of the sequence (
arr
.length
).
A new array containing the extracted elements.
slice
does not alter the original array. It returns a shallow copy of elements from the original array. Elements of the original array are copied into the returned array as follows:
slice
copies 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.
String
,
Number
and
布尔
objects),
slice
copies the values into the new array. Changes to the string, number, or boolean in one array do not affect the other array.
If a new element is added to either array, the other array is not affected.
let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'] let citrus = fruits.slice(1, 3) // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'] // citrus contains ['Orange','Lemon']
In the following example,
slice
creates a new array,
newCar
,从
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.
let myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }
let myCar = [myHonda, 2, 'cherry condition', 'purchased 1997']
let newCar = myCar.slice(0, 2)
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
console.log('myCar = ' + JSON.stringify(myCar))
console.log('newCar = ' + JSON.stringify(newCar))
console.log('myCar[0].color = ' + myCar[0].color)
console.log('newCar[0].color = ' + newCar[0].color)
// Change the color of myHonda.
myHonda.color = 'purple'
console.log('The new color of my Honda is ' + myHonda.color)
// Display the color of myHonda referenced from both arrays.
console.log('myCar[0].color = ' + myCar[0].color)
console.log('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
slice
method can also be called to convert Array-like objects/collections to a new Array. You just
bind
the method to the object. The
arguments
inside a function is an example of an 'array-like object'.
function list() {
return Array.prototype.slice.call(arguments)
}
let list1 = list(1, 2, 3) // [1, 2, 3]
Binding can be done with the
call()
方法为
Function.prototype
and it can also be reduced using
[].slice.call(arguments)
而不是
Array.prototype.slice.call
.
Anyway, it can be simplified using
bind
.
let unboundSlice = Array.prototype.slice
let slice = Function.prototype.call.bind(unboundSlice)
function list() {
return slice(arguments)
}
let list1 = list(1, 2, 3) // [1, 2, 3]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.prototype.slice' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
slice
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Array
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()