Phương thức Slice [] sao chép một phần nhất định của một mảng và trả về phần sao chép phần mới. Nó không thay đổi mảng ban đầu.copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array.
Phương thức splice [] thay đổi một mảng, bằng cách thêm hoặc loại bỏ các phần tử khỏi nó.changes an array, by adding or removing elements from it.
Đây là cú pháp lát cắt:slice syntax:
array.slice[from, until];
// example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.slice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 2, 3, 4, 5, 6], newArray: [2, 3]
Lưu ý: Phương thức Slice [] cũng có thể được sử dụng cho các chuỗi.
Và đây là cú pháp mối nối:splice syntax:
//For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
Lưu ý: Nếu chúng tôi không xác định tham số thứ hai, mọi phần tử bắt đầu từ chỉ mục đã cho sẽ bị xóa khỏi mảng
// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
Liên kết liên quan:
Hãy để rõ ràng sự nhầm lẫn xung quanh các phương thức Slice [], splice [], & split [] trong javascript
Array.prototype.slice[]
Array.prototype.splice[]
Dillion Megida
Một sự khác biệt giữa các phương thức splice [] và lát [] là gì?
slice
Phương thức Slice [] sao chép một phần nhất định của một mảng và trả về phần sao chép phần mới. Nó không thay đổi mảng ban đầu. Phương thức splice [] thay đổi một mảng, bằng cách thêm hoặc loại bỏ các phần tử khỏi nó. Lưu ý: Phương thức Slice [] cũng có thể được sử dụng cho các chuỗi.
Cú pháp
array.slice[start, end]
1 biểu thị chỉ mục mà phương thức sẽ bắt đầu hoạt động của nó trên mảng.//For removing elements, we need to give the index parameter, // and the number of elements to be removed array.splice[index, number of elements to be removed]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3] console.log[{array, newArray}] // output: array: [1, 5, 6], newArray: [2, 3, 4]
8 biểu thị số lượng giá trị sẽ bị xóa khỏi// For adding elements, we need to give them as the 3rd, 4th, ... parameter array.splice[index, number of elements to be removed, element, element]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3, 'a', 'b'] console.log[{array, newArray}] // output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
1. Nếu giá trị là//For removing elements, we need to give the index parameter, // and the number of elements to be removed array.splice[index, number of elements to be removed]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3] console.log[{array, newArray}] // output: array: [1, 5, 6], newArray: [2, 3, 4]
0, sẽ không có gì bị xóa.array.slice[start, end]
Giá trị trả về
Giá trị trả về là các giá trị bị ảnh hưởng, tức là đã xóa. Nếu
// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
8 là array.slice[start, end]
0, một mảng trống sẽ được trả về.excluding the value at //For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
2.Nếu
//For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
2 không được chỉ định, độ dài của mảng sẽ được sử dụng. Do đó, tất cả các giá trị từ chỉ số bắt đầu sẽ được trả về.
Thí dụ
let arr = [
"educative",
4,
[1,3],
{type: "animal"}
];
let slicedValues = arr.slice[1,3];
console.log[arr];
console.log[slicedValues];
Như đã nêu trong cú pháp trên,
//For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
7 là chỉ số bắt đầu và //For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
8 là chỉ số kết thúc.Tại
//For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
9, giá trị là // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
0. Tại // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
1, giá trị là // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
2. Các giá trị giữa hai giá trị này là // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
3. Do đó, giá trị trả lại sẽ là Subarray // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
4. Hãy nhớ rằng, giá trị tại chỉ số cuối cùng được loại trừ.Nhưng
// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
5 giữ lại các giá trị của nó.splice
Splice, theo từ điển, có nghĩa là tham gia mọi thứ với nhau. Nó được sử dụng để loại bỏ các phần tử khỏi một mảng hoặc thay thế chúng.
Cú pháp
array.splice[start, deleteCount, newElem1, newElem2, ..., newElemN;
1 biểu thị chỉ mục mà phương thức sẽ bắt đầu hoạt động của nó trên mảng.//For removing elements, we need to give the index parameter, // and the number of elements to be removed array.splice[index, number of elements to be removed]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3] console.log[{array, newArray}] // output: array: [1, 5, 6], newArray: [2, 3, 4]
8 biểu thị số lượng giá trị sẽ bị xóa khỏi// For adding elements, we need to give them as the 3rd, 4th, ... parameter array.splice[index, number of elements to be removed, element, element]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3, 'a', 'b'] console.log[{array, newArray}] // output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
1. Nếu giá trị là//For removing elements, we need to give the index parameter, // and the number of elements to be removed array.splice[index, number of elements to be removed]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3] console.log[{array, newArray}] // output: array: [1, 5, 6], newArray: [2, 3, 4]
0, sẽ không có gì bị xóa.array.slice[start, end]
1 đếnarray.slice[start, end]
2 biểu thị các giá trị sẽ được thêm vào sauarray.slice[start, end]
1.//For removing elements, we need to give the index parameter, // and the number of elements to be removed array.splice[index, number of elements to be removed]; //example let array = [1, 2, 3, 4, 5, 6] let newArray = array.splice[1, 3] console.log[{array, newArray}] // output: array: [1, 5, 6], newArray: [2, 3, 4]
Giá trị trả về
Giá trị trả về là các giá trị bị ảnh hưởng, tức là đã xóa. Nếu
// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
8 là array.slice[start, end]
0, một mảng trống sẽ được trả về.Thí dụ
let arr = [
"educative",
4,
[1,3],
{type: "animal"}
];
let returnedArr = arr.splice[2,1,{name: "educative"}];
console.log[arr];
console.log[returnedArr];
// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
5, mảng ban đầu, được sửa đổi và trở thành:[
"educative",
4,
{name: "educative"},
{type: "animal"}
]
//For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
1 là array.slice[start, end]
8, // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
8 là //For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
7, array.slice[start, end]
1 là 2. Tạilet arr = [
"educative",
4,
[1,3],
{type: "animal"}
];
let slicedValues = arr.slice[1,3];
console.log[arr];
console.log[slicedValues];
3, giá trị làlet arr = [
"educative",
4,
[1,3],
{type: "animal"}
];
let slicedValues = arr.slice[1,3];
console.log[arr];
console.log[slicedValues];
// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
3. //For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
7 Mục bị xóa khỏi mảng là // For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice[index, number of elements to be removed, element, element];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3, 'a', 'b']
console.log[{array, newArray}]
// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]
3. array.slice[start, end]
1 sau đó được thêm vào ngay sau //For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
1. Vì //For removing elements, we need to give the index parameter,
// and the number of elements to be removed
array.splice[index, number of elements to be removed];
//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice[1, 3]
console.log[{array, newArray}]
// output: array: [1, 5, 6], newArray: [2, 3, 4]
1 bị loại bỏ, array.splice[start, deleteCount, newElem1, newElem2, ..., newElemN;
0 thay thế nó.array.splice[start, deleteCount, newElem1, newElem2, ..., newElemN;
1 là array.splice[start, deleteCount, newElem1, newElem2, ..., newElemN;
2, chứa giá trị bị ảnh hưởng.Gói [lại
slice
trả về một phần của mảng nhưng nó không ảnh hưởng đến mảng ban đầu. splice
thay đổi mảng ban đầu bằng cách xóa, thay thế hoặc thêm các giá trị và trả về các giá trị bị ảnh hưởng.
Khi bạn sử dụng mỗi người là tùy thuộc vào bạn. Nếu bạn phải thay đổi nội dung của mảng, splice
là cách để đi, nhưng nếu bạn chỉ muốn có được một SubArray, slice
nên là lựa chọn của bạn.
Cảm ơn vì đã đọc :]
THẺ LIÊN QUAN
JavaScript
mảng
cộng đồng
Người đóng góp