The slice() method returns a shallow copy of a portion of an array inlớn a new array object selected from start khổng lồ kết thúc (over not included) where start and over represent the index of items in that array. The original array will not be modified.
Bạn đang xem: Camel là gì
start Optional Zero-based index at which to lớn start extraction.
A negative index can be used, indicating an offset from the over of the sequence. slice(-2) extracts the last two elements in the sequence.
If start is undefined, slice starts from the index 0.
If start is greater than the index range of the sequence, an empty array is returned.end Optional
Zero-based index before which lớn end extraction. slice extracts up to but not including kết thúc. For example, 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 over of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence.
If over is omitted, slice extracts through the kết thúc of the sequence (arr.length).
If kết thúc is greater than the length of the sequence, slice extracts through tothe over 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 lớn the returned array as follows:For objects,slice copies object references into the new array. Both the original and new array refer lớn the same object. If an object changes, the changes are visible khổng lồ both the new và original arrays.
If a new element is added to lớn either array, the other array is not affected.
Xem thêm: Tài Linh - Nghệ Sĩ Chí Linh
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">
In the following example, slice creates a new array, newCar, from myCar. Both include a reference to lớn the object myHondomain authority. 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 =
This script writes:
slice method can also be called to convert Array-lượt thích objects/collections to a new Array. You just bind the method khổng lồ the object. The arguments inside a function is an example of an "array-lượt thích 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() method of Function & it can also be reduced using <>.slice.call(arguments) instead of Array.prototype.slice.call.
Anyway, it can be simplified using bind.
let unboundSlice = Array.prototype.slicelet slice = Function.prototype.hotline.bind(unboundSlice)function list() return slice(arguments)let list1 = list(1, 2, 3) // <1, 2, 3>
|ECMAScript Language Specification (ECMAScript)# sec-array.prototype.slice|
Browser compatibilityBCD tables only load in the browser
Found a problem with this page?
Last modified: Oct 14, 2021, by MDoanh Nghiệp contributorsChange your languageSelect your preferred language English (US)DeutschEspañolFrançais日本語한국어PolskiPortuguês (doBrasil)Русский中文 (简体)正體中文 (繁體) Change language