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.

The source for this interactive demo is stored in a GitHub repository. If you'd like to contribute to the interactive demo project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

arr.slice([start[, end]])
					

参数

start 可选

Zero-based index at which to start extraction.

A negative index can be used, indicating an offset from the end of the sequence. 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 可选
Zero-based index before which to end extraction. 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).
A negative index can be used, indicating an offset from the end of the sequence. 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:

  • For object references (and not the actual object), 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.
  • For strings, numbers and booleans (not 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.

范例

Return a portion of an existing array

let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
let citrus = fruits.slice(1, 3)
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
					

Using slice>

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
					

Array-like objects

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.

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
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

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Array
  3. 特性
    1. Array.prototype.length
    2. Array.prototype[@@unscopables]
  4. 方法
    1. Array.from()
    2. Array.isArray()
    3. Array.of()
    4. Array.prototype.concat()
    5. Array.prototype.copyWithin()
    6. Array.prototype.entries()
    7. Array.prototype.every()
    8. Array.prototype.fill()
    9. Array.prototype.filter()
    10. Array.prototype.find()
    11. Array.prototype.findIndex()
    12. Array.prototype.flat()
    13. Array.prototype.flatMap()
    14. Array.prototype.forEach()
    15. Array.prototype.includes()
    16. Array.prototype.indexOf()
    17. Array.prototype.join()
    18. Array.prototype.keys()
    19. Array.prototype.lastIndexOf()
    20. Array.prototype.map()
    21. Array.prototype.pop()
    22. Array.prototype.push()
    23. Array.prototype.reduce()
    24. Array.prototype.reduceRight()
    25. Array.prototype.reverse()
    26. Array.prototype.shift()
    27. Array.prototype.slice()
    28. Array.prototype.some()
    29. Array.prototype.sort()
    30. Array.prototype.splice()
    31. Array.prototype.toLocaleString()
    32. Array.prototype.toSource()
    33. Array.prototype.toString()
    34. Array.prototype.unshift()
    35. Array.prototype.values()
    36. Array.prototype[@@iterator]()
    37. get Array[@@species]
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  11. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()