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).

Return value

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">

Using slice

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 = let newCar = myCar.slice(0, 2)// Display the values of myCar, newCar, & 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 Hondomain authority is " + myHondomain authority.color)// Display the color of myHondomain authority referenced from both arrays.console.log("myCar<0>.color = " + myCar<0>.color)console.log("newCar<0>.color = " + newCar<0>.color)
This script writes:

myCar = newCar = myCar<0>.color = rednewCar<0>.color = redThe new color of my Hondomain authority is purplemyCar<0>.color = purplenewCar<0>.color = purple

Array-like objects

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 list1 = list(1, 2, 3) // <1, 2, 3>
Binding can be done with the call() method of Function & it can also be reduced using <> instead of

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 compatibility

BCD tables only load in the browser

See also

Found a problem with this page?

Last modified: Oct 14, 2021, by MDoanh Nghiệp contributors

Change your languageSelect your preferred language English (US)DeutschEspañolFrançais日本語한국어PolskiPortuguês (doBrasil)Русский中文 (简体)正體中文 (繁體) Change language
Related Topics
Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *