Hướng dẫn what is difference between splice and slice method in javascript? - sự khác biệt giữa phương thức splice và slice trong javascript là gì?

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)
  • //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 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 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 biểu thị số lượng giá trị sẽ bị xóa khỏ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]
    
    1. Nếu giá trị là
    array.slice(start, end)
    
    0, sẽ không có gì bị xóa.

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;
  • //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 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 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 biểu thị số lượng giá trị sẽ bị xóa khỏ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]
    
    1. Nếu giá trị là
    array.slice(start, end)
    
    0, sẽ không có gì bị xóa.
  • array.slice(start, end)
    
    1 đến
    array.slice(start, end)
    
    2 biểu thị các giá trị sẽ được thêm vào 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.

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à

let arr = [

"educative",

4,

[1,3],

{type: "animal"}

];

let slicedValues = arr.slice(1,3);

console.log(arr);

console.log(slicedValues);

2. Tại

let arr = [

"educative",

4,

[1,3],

{type: "animal"}

];

let slicedValues = arr.slice(1,3);

console.log(arr);

console.log(slicedValues);

3, 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]

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

Hướng dẫn what is difference between splice and slice method in javascript? - sự khác biệt giữa phương thức splice và slice trong javascript là gì?
Dillion Megida

Một sự khác biệt giữa các phương thức splice () và lát () là gì?

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.The splice( ) method changes an array, by adding or removing elements from it. Note: the Slice( ) method can also be used for strings.

Sự khác biệt giữa các phương thức mảng cắt lát và mối nối là gì?

Cắt lát trả về một phần của mảng nhưng nó không ảnh hưởng đến mảng gốc.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. splice changes the original array by removing, replacing, or adding values and returns the affected values. When you use each one is up to you.

Phương pháp lát cắt trong JavaScript là gì?

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ừ đầu đến cuối (không bao gồm kết thúc) trong đó bắt đầu và kết thúc biểu thị chỉ mục của các mục trong mảng đó.Mảng ban đầu sẽ không được sửa đổi.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.

Làm thế nào để bạn nhớ lát cắt và mối nối?

Tôi nhớ sự khác biệt giữa lát cắt và mối nối bằng cách sử dụng một bản ghi nhớ.Splice có thêm một chữ cái, 'P', so với lát cắt.Do chữ cái bổ sung, tôi liên kết chữ cái bổ sung để sử dụng thêm hoặc xóa khỏi mảng gốc.splice has an extra letter, 'p', compared to slice . Because of the extra letter, I associate the additional letter to splice 's use of adding or removing from the original array.