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ừ start
đến end
[không bao gồm end
] trong đó start
và end
đạ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 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. Thử nó
Cú pháp
slice[]
slice[start]
slice[start, end]
Thông số
start
Tùy chọnOptional
Chỉ số dựa trên không tại đó để bắt đầu khai thác.
Một chỉ số âm có thể được sử dụng, cho biết độ lệch từ cuối chuỗi.
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice[1, 3];
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
4 trích xuất hai yếu tố cuối cùng trong chuỗi. Nếu start
không được xác định,
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice[1, 3];
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 sẽ bắt đầu từ chỉ mụ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']
7.
Nếu start
lớn hơn phạm vi chỉ mục của chuỗi, một mảng trống được trả về.
end
Tùy chọnOptionalChỉ số của phần tử đầu tiên để loại trừ khỏi mảng được trả 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']
6 chiết xuất lên đến nhưng không bao gồm end
. Ví dụ, // 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];
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
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];
// 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];
2 trích xuất phần tử thứ hai thông qua phần tử thứ tư [các phần tử được lập chỉ mục 1, 2 và 3].Một chỉ số âm có thể được sử dụng, cho biết độ lệch từ cuối chuỗ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];
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
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];
// 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];
3 trích xuất phần tử thứ ba thông qua phần tử thứ hai đến cuối cùng trong chuỗi. Nếu end
bị bỏ qua,
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice[1, 3];
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 trích xuất qua phần cuối của chuỗ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];
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
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];
// 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];
6]. Nếu end
lớn hơn chiều dài của chuỗi,
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice[1, 3];
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 chiết xuất cho đến cuối chuỗ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];
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
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];
// 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];
6].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 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 = purple1 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 = purple1 có thuộc tính
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 = purple5 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 fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice[1, 3];
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
6 tạo ra một mảng mớ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 = purple7, 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 = purple8. Cả hai đều bao gồm một tham chiếu đến đối tượng
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 = purple9. Khi màu 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 = purple9 đượ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];
// Display the values of myCar, newCar, and the color of myHonda
// referenced from both arrays.
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];
// 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];
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 = purple
Gọ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
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 = purple5 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 = purple1. Sau đó, nó đọc các thuộc tính được khóa nguyên từ
start
đến end
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
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
};
console.log[Array.prototype.slice.call[arrayLike, 1, 3]];
// [ 3, 4 ]
7 và const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
};
console.log[Array.prototype.slice.call[arrayLike, 1, 3]];
// [ 3, 4 ]
8 để 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