Ví dụ
Chọn các yếu tố:
const fruits = ["chuối", "cam", "chanh", "táo", "xoài"]; const citrus = trái cây.slice (1, 3);
const citrus = fruits.slice(1, 3);
Hãy tự mình thử »
Chọn các phần tử bằng cách sử dụng các giá trị âm:
const fruits = ["chuối", "cam", "chanh", "táo", "xoài"]; const mybest = fruits.slice (-3, -1);
const myBest = fruits.slice(-3, -1);
Hãy tự mình thử »
Chọn các phần tử bằng cách sử dụng các giá trị âm:
const fruits = ["chuối", "cam", "chanh", "táo", "xoài"]; const mybest = fruits.slice (-3, -1);
Định nghĩa và cách sử dụng
Phương thức slice() trả về các phần tử được chọn trong một mảng, dưới dạng mảng mới.
Phương pháp slice() chọn từ một khởi đầu nhất định, cho đến một (không bao gồm) kết thúc được đưa ra.
Phương pháp slice() không thay đổi mảng gốc.
Cú pháp | Thông số |
Tham số | Sự mô tả Start position. Default is 0. Negative numbers select from the end of the array. |
bắt đầu | Tùy chọn. Vị trí khởi động. Mặc định là 0. Số âm Chọn từ cuối mảng. End position. Default is last element. Negative numbers select from the end of the array. |
chấm dứt
Tùy chọn. Vị trí. Mặc định là yếu tố cuối cùng. Số âm Chọn từ cuối mảng.
Giá trị trả về
Một mảng mới chứa các yếu tố đã chọn.
Hỗ trợ trình duyệt
slice() là tính năng ECMAScript1 (ES1). | ES1 (JavaScript 1997) được hỗ trợ đầy đủ trong tất cả các trình duyệt: | Trình duyệt Chrome | I E | Bờ rìa | Firefox |
Cuộc đi săn | Cuộc đi săn | Cuộc đi săn | Cuộc đi săn | Cuộc đi săn | Cuộc đi săn |
Phương thức slice() trả về một bản sao nông của một phần của một mảng vào một đối tượng mảng mới được chọn từ const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
2 đến const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3 (không bao gồm const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3) trong đó const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
2 và const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3 đại diện cho chỉ số của các mục trong mảng đó. Mảng ban đầu sẽ không được sửa đổi.slice() method returns a shallow copy of a portion of an array into a new array object selected from const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
2 to const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3 (const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3 not included) where const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
2 and const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3 represent the index of items in that array. The original array will not be modified. slice()
slice(start)
slice(start, end)
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
2 Tùy chọnOptional Chỉ số dựa trên không tại đó để bắt đầu trích xuất, được chuyển đổi thành một số nguyên. Chỉ số dựa trên không tại đó để kết thúc trích xuất, được chuyển đổi thành một số nguyên. slice() trích xuất đến nhưng không bao gồm const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3.Thử nó
Cú pháp
Thông số
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
3 Tùy chọnOptional
Giá trị trả về
Một mảng mới chứa các yếu tố trích xuất.
Sự mô tả
Phương pháp slice() là một phương thức sao chép. Nó không làm thay đổi 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 7 mà thay vào đó trả về một bản sao nông chứa một số phần tử giống như các phần tử từ mảng gốc.
Phương pháp slice() bảo tồn các khe trống. Nếu phần cắt lát thưa thớt, mảng trả về cũng thưa thớt.
Phương pháp slice() là chung chung. Nó chỉ mong đợi giá trị 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 7 có thuộc tính const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 1 và các thuộc tính được khóa.
Ví dụ
Trả về một phần của một mảng hiện có
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']; const citrus = fruits.slice(1, 3); // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'] // citrus contains ['Orange','Lemon']
Sử dụng lát cắt
Trong ví dụ sau, const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 2 tạo ra một mảng mới, const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 3, từ const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 4. Cả hai đều bao gồm một tham chiếu đến đối tượng const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 5. Khi màu của const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 5 được thay đổi thành màu tím, cả hai mảng đều phản ánh sự thay đổi.
// Using slice, create newCar from myCar. const myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }; const myCar = [myHonda, 2, 'cherry condition', 'purchased 1997']; const newCar = myCar.slice(0, 2); console.log('myCar =', myCar); console.log('newCar =', 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); console.log('myCar[0].color =', myCar[0].color); console.log('newCar[0].color =', newCar[0].color);
Kịch bản này viết:
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 = purpleGọi lát () trên các đối tượng không phải là
Phương pháp slice() đọc thuộc tính const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ] 1 của 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 7. Sau đó, nó đọc các thuộc tính được khóa nguyên từ const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']; const citrus = fruits.slice(1, 3); // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'] // citrus contains ['Orange','Lemon'] 2 đến const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']; const citrus = fruits.slice(1, 3); // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'] // citrus contains ['Orange','Lemon'] 3 và xác định chúng trên một mảng mới được tạo.
const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, }; console.log(Array.prototype.slice.call(arrayLike, 1, 3)); // [ 3, 4 ]
Sử dụng Slice () để chuyển đổi các đối tượng giống như mảng thành mảng
Phương pháp slice() thường được sử dụng với // slice() is called with `this` passed as the first argument const slice = Function.prototype.call.bind(Array.prototype.slice); function list() { return slice(arguments); } const list1 = list(1, 2, 3); // [1, 2, 3] 3 và // slice() is called with `this` passed as the first argument const slice = Function.prototype.call.bind(Array.prototype.slice); function list() { return slice(arguments); } const list1 = list(1, 2, 3); // [1, 2, 3] 4 để tạo phương thức tiện ích chuyển đổi một đối tượng giống như mảng thành một mảng.
// slice() is called with `this` passed as the first argument const slice = Function.prototype.call.bind(Array.prototype.slice); function list() { return slice(arguments); } const list1 = list(1, 2, 3); // [1, 2, 3]
Sử dụng Slice () trên các mảng thưa thớt
Mảng được trả về từ slice() có thể thưa thớt nếu nguồn thưa thớt.
console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]
Thông số kỹ thuật
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-array.prototype.slice # sec-array.prototype.slice |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt