Hướng dẫn splice javascript – javascript nối

Bài này chúng ta sẽ tìm hiểu hàm splice trong javascript. Splice javascript là hàm dùng để thay thế một số phần tử trong mảng bằng phần tử khác.Splice javascript là hàm dùng để thay thế một số phần tử trong mảng bằng phần tử khác.

Nội dung chính

Show

  • 1. Hàm splice trong javascript là gì?
  • 2. Các ví dụ với hàm splice trong javascript
  • Definition and Usage
  • Browser Support
  • 2. Các ví dụ với hàm splice trong javascript
  • Return Value
  • More Examples
  • Browser Support
  • Definition and Usage
  • Browser Support
  • 2. Các ví dụ với hàm splice trong javascript
  • Return Value
  • More Examples
  • Browser Support
  • 1. Hàm splice trong javascript là gì?
  • 2. Các ví dụ với hàm splice trong javascript
  • 1. Định nghĩa nhanh về splice
  • 2. Một số ví dụ cụ thể
  • 3.Tổng kết lại

Hướng dẫn splice javascript - javascript nối

Hướng dẫn splice javascript - javascript nối

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Hàm splice sẽ thay thế một hoặc một số phần tử của mảng bằng một hoặc một số phần tử khác, lưu ý rằng số phần tử bị bỏ đi có thể ít hơn số phần tử được thêm vào và ngược lại.

Splice javascript sẽ thay đổi các phần tử trong mảng bằng cách xóa bỏ hoặc thay thế các phần tử hiện có, hoặc thêm phần tử mới vào vị trí xác định.

Nếu bạn muốn tham khảo bằng một ví dụ đơn giản thì hãy xem ví dụ dưới đây.

Bài viết này được đăng tại [free tuts .net]

// Lưu ý: Chỉ mục bắt đầu từ 0. Vị trí 1 tức là chỉ mục 1 => là phần tử thứ 2

let months = ['Jan', 'March', 'April', 'June'];

// Thêm tại vị trí 1, không xóa phần tử nào.
months.splice(1, 0, 'Feb');
console.log(months);
// output: ["Jan", "Feb", "March", "April", "June"]

months = ['Jan', 'March', 'April', 'June'];

// Xóa 2 phần tử bắt đầu từ vị trí 1 (tức phần tử thứ 2),
// Lúc này mảng sẽ còn ['Jan', 'June']
// Sau đó thêm vào vị trí 1 phần tử "may"
months.splice(1, 2, 'May');

console.log(months);
// output: ["Jan", "May", "June"]

1. Hàm splice trong javascript là gì?

Splice javascript là hàm dùng để xóa các phần tử trong mảng, hoặc thay thế một phần tử trong mảng thành một hoặc nhiều phần tử khác. Nói cách khác: là hàm dùng để xóa các phần tử trong mảng, hoặc thay thế một phần tử trong mảng thành một hoặc nhiều phần tử khác. Nói cách khác:

  • Bạn có thể dùng hàm array splice để xóa phần tử.
  • Hoặc dùng để bổ sung phần tử vào một vị trí nào đó trong mảng.

Cú pháp như sau::

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

Trong đó:

  • splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    6 – là vị trí chỉ mục bắt đầu thay thế.

  • splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    7 – là số phần tử sẽ bị lại bỏ, tính tử vị trí chỉ mục

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    6, và bao gồm cả phần tử

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    6. Nếu

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    6 mang giá trị 0 thì sẽ không có phần tử nào bị loại bỏ.

  • let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
    let removed = myFish.splice(2, 0, 'drum')
    
    console.log(myFish);
    // Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]
    
    console.log(removed);
    // Kết quả []

    1 – các phần tử sẽ được thêm vào từ vị trí

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    6. Sau khi thêm, phần tử thứ

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    6 của mảng sẽ là

    let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
    let removed = myFish.splice(2, 0, 'drum')
    
    console.log(myFish);
    // Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]
    
    console.log(removed);
    // Kết quả []

    4.

Giá trị trả về: Là một mảng chứa tất cả những phần tử đã bị xóa. Nếu chỉ xóa một phần tử thì sẽ return về mảng có 1 phần tử. Nếu không có phần tử nào bị xóa thì sẽ return về mảng rỗng.: Là một mảng chứa tất cả những phần tử đã bị xóa. Nếu chỉ xóa một phần tử thì sẽ return về mảng có 1 phần tử. Nếu không có phần tử nào bị xóa thì sẽ return về mảng rỗng.

2. Các ví dụ với hàm splice trong javascript

Ví dụ 1: Sử dụng splice javascript để bổ sung một phần tử vào vị trí chỉ mục 2, không xóa phần tử nào.: Sử dụng splice javascript để bổ sung một phần tử vào vị trí chỉ mục 2, không xóa phần tử nào.

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum')

console.log(myFish);
// Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]

console.log(removed);
// Kết quả []

Ví dụ 2: Cũng tại vị trí chỉ mục 2, hãy bổ sung thêm hai phần tử.: Cũng tại vị trí chỉ mục 2, hãy bổ sung thêm hai phần tử.

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum', 'guitar')

console.log(myFish )
// Kết quả: ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]

console.log(removed)
// Kết quả: []

Ví dụ 3: Xóa 1 phần tử tại chỉ mục 2, và thêm vào phần tử “trumpet”: Xóa 1 phần tử tại chỉ mục 2, và thêm vào phần tử “trumpet”

let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
let removed = myFish.splice(2, 2)

console.log(myFish )
// Kết quả:["parrot", "anemone", "sturgeon"]

console.log(removed)
// Kết quả:["blue", "trumpet"]

Ví dụ 4: Sử dụng phương thức splice để thay thế một đoạn của mảng.: Sử dụng phương thức splice để thay thế một đoạn của
mảng.

<button onclick="myFunction()">Run</button>

<p id="demo"></p>

<script>
    var subject = ["php", "css", "html", "js"];
    document.getElementById("demo").innerHTML = subject;

    function myFunction() {
        subject.splice(1, 1, "python", "c#", "ios");
        document.getElementById("demo").innerHTML = subject;
    }
</script>

Như vậy là mình đã hướng dẫn xong cách sử dụng hàm splice trong javascript.

Examples

At position 2, add 2 elements:

Nội dung chính

  • Definition and Usage
  • Return Value
  • More Examples
  • Browser Support
  • Definition and Usage
  • Return Value
  • More Examples
  • Browser Support
  • 1. Hàm splice trong javascript là gì?
  • 2. Các ví dụ với hàm splice trong javascript
  • 1. Định nghĩa nhanh về splice
  • 2. Một số ví dụ cụ thể
  • 3.Tổng kết lại

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];

fruits.splice(2, 0, “Lemon”, “Kiwi”);

Try it Yourself »

At position 2, remove 2 items:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);
fruits.splice(2, 2);

Try it Yourself »

Definition and Usage

Return Value

More Examples

Browser Support

1. Hàm splice trong javascript là gì?

2. Các ví dụ với hàm splice trong javascript

1. Định nghĩa nhanh về splice2. Một số ví dụ cụ thể3.Tổng kết lại const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
The position to add/remove items.
Negative value defines the position from the end of the array.
fruits.splice(2, 0, “Lemon”, “Kiwi”); Try it Yourself »
Number of items to be removed.
At position 2, remove 2 items: const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);
New elements(s) to be added.

Return Value

More Examples

More Examples

Browser Support

1. Hàm splice trong javascript là gì?
fruits.splice(2, 1, “Lemon”, “Kiwi”);

Try it Yourself »

Browser Support

1. Hàm splice trong javascript là gì?

2. Các ví dụ với hàm splice trong javascript

1. Định nghĩa nhanh về splice2. Một số ví dụ cụ thể3.Tổng kết lại const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.splice(2, 0, “Lemon”, “Kiwi”); Try it Yourself »At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:

Examples

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);

The

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum')

console.log(myFish);
// Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]

console.log(removed);
// Kết quả []

5 method adds and/or removes array elements.
let result = text.slice(0, 5);

Try it Yourself »

At position 2, remove 2 items:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);

Try it Yourself »

At position 2, remove 2 items:

Definition and Usage

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);

The

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum')

console.log(myFish);
// Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]

console.log(removed);
// Kết quả []

5 method adds and/or removes array elements.

The

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum')

console.log(myFish);
// Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]

console.log(removed);
// Kết quả []

5 method overwrites the original array.

Syntax

array.splice(index, howmany, item1, ….., itemX)

Parameters

Browser Support

2. Các ví dụ với hàm splice trong javascript

1. Định nghĩa nhanh về splice2. Một số ví dụ cụ thể3.Tổng kết lại const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
The start position.
(First character is 0).
fruits.splice(2, 0, “Lemon”, “Kiwi”); Try it Yourself »
The end position (up to, but not including).
Default is string length.

Return Value

At position 2, remove 2 items:2. Một số ví dụ cụ thể3.Tổng kết lại const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];

More Examples

Browser Support

fruits.splice(2, 0, “Lemon”, “Kiwi”);

2. Các ví dụ với hàm splice trong javascript

1. Định nghĩa nhanh về splice2. Một số ví dụ cụ thể3.Tổng kết lại const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.splice(2, 0, “Lemon”, “Kiwi”); Try it Yourself »At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:At position 2, remove 2 items:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);Splice javascript là hàm dùng để thay thế một số phần tử trong mảng bằng phần tử khác.

Nội dung chính

  • 1. Hàm splice trong javascript là gì?
  • 2. Các ví dụ với hàm splice trong javascript
  • 1. Định nghĩa nhanh về splice
  • 2. Một số ví dụ cụ thể
  • 3.Tổng kết lại

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];freetuts.net, không được copy dưới mọi hình
thức.

fruits.splice(2, 0, “Lemon”, “Kiwi”);

Try it Yourself »

At position 2, remove 2 items:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);

// Lưu ý: Chỉ mục bắt đầu từ 0. Vị trí 1 tức là chỉ mục 1 => là phần tử thứ 2

let months = ['Jan', 'March', 'April', 'June'];

// Thêm tại vị trí 1, không xóa phần tử nào.
months.splice(1, 0, 'Feb');
console.log(months);
// output: ["Jan", "Feb", "March", "April", "June"]

months = ['Jan', 'March', 'April', 'June'];

// Xóa 2 phần tử bắt đầu từ vị trí 1 (tức phần tử thứ 2),
// Lúc này mảng sẽ còn ['Jan', 'June']
// Sau đó thêm vào vị trí 1 phần tử "may"
months.splice(1, 2, 'May');

console.log(months);
// output: ["Jan", "May", "June"]

1. Hàm splice trong javascript là gì?

2. Các ví dụ với hàm splice trong javascript là hàm dùng để xóa các phần tử trong mảng, hoặc thay thế một phần tử trong mảng thành một hoặc nhiều phần tử khác. Nói cách khác:

  • 1. Định nghĩa nhanh về splice
  • 2. Một số ví dụ cụ thể

3.Tổng kết lại:

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];

  • fruits.splice(2, 0, “Lemon”, “Kiwi”);
  • Try it Yourself »
  • At position 2, remove 2 items:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”]; fruits.splice(2, 2);: Là một mảng chứa tất cả những phần tử đã bị xóa. Nếu chỉ xóa một phần tử thì sẽ return về mảng có 1 phần tử. Nếu không có
phần tử nào bị xóa thì sẽ return về mảng rỗng.

2. Các ví dụ với hàm splice trong javascript

Ví dụ 1: Sử dụng splice javascript để bổ sung một phần tử vào vị trí chỉ mục 2, không xóa phần tử nào.: Sử dụng splice javascript để bổ sung một phần tử vào vị trí chỉ mục 2, không xóa phần tử nào.

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum')

console.log(myFish);
// Kết quả: ["angel", "clown", "drum", "mandarin", "sturgeon"]

console.log(removed);
// Kết quả []

Ví dụ 2: Cũng tại vị trí chỉ mục 2, hãy bổ sung thêm hai phần tử.: Cũng tại vị trí chỉ mục 2, hãy bổ sung thêm hai phần tử.

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum', 'guitar')

console.log(myFish )
// Kết quả: ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]

console.log(removed)
// Kết quả: []

Ví dụ 3: Xóa 1 phần tử tại chỉ mục 2, và thêm vào phần tử “trumpet”: Xóa 1 phần tử tại chỉ mục 2, và thêm vào phần tử “trumpet”

let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
let removed = myFish.splice(2, 2)

console.log(myFish )
// Kết quả:["parrot", "anemone", "sturgeon"]

console.log(removed)
// Kết quả:["blue", "trumpet"]

Ví dụ 4: Sử dụng phương thức splice để thay thế một đoạn của mảng.: Sử dụng phương thức splice để thay thế một đoạn của mảng.

<button onclick="myFunction()">Run</button>

<p id="demo"></p>

<script>
    var subject = ["php", "css", "html", "js"];
    document.getElementById("demo").innerHTML = subject;

    function myFunction() {
        subject.splice(1, 1, "python", "c#", "ios");
        document.getElementById("demo").innerHTML = subject;
    }
</script>

Như vậy là mình đã hướng dẫn xong cách sử dụng hàm splice trong javascript.

1. Định nghĩa nhanh về splice

  • Nếu như
    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    1,

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    2,

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    3 &

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    4 cho phép chúng ta lấy add hay remove item từ vị trí đầu tiên hoặc cuối cùng của array, thì splice được ví như là “a swiss army knife” cho phép chúng ta có thể tự do insert, remove & replace bao nhiêu item tuỳ ý.

  • Kết quả sau khi thực hiện method này sẽ làm thay đổi array gốc & lấy ra được các item mà chúng ta remove.

2. Một số ví dụ cụ thể

2.1 Remove – Xoá bỏ một hoặc nhiều phần tử trong array

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

2

  • Sau khi chạy ví dụ trên, các bạn đã hiểu hoàn toàn chưa? Nếu chưa hay để lại cho mình một comment ở cuối bài viết nhé.
  • Array.splice với một tham số (param) được cung cấp điều này đồng nghĩa với việc: array sẽ được cut ở index thứ n với duy nhất 1 item. Còn nếu như chúng ta muốn cut nhiều item thì sao nhỉ ?
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

3

  • Như các bạn đã thấy param thứ 2 của splice đã xuất hiện với ý nghĩa là “số lượng item cần xoá”.

**2.2 Add một hoặc nhiều item vào giữa array **

  • Hãy tiếp tục với một ví dụ sử dụng
    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    5 để thêm item vào array nào.

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    4

  • Có thể lúc này một số bạn phần lớn đã nhận thấy sự xuất hiện của param thứ 3. Khi làm việc với splice, khi bạn cần
    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    6 thêm item vào mảng hãy nhớ rằng truyền item đó vào ở param thứ 3 trở đi. Nhưng tại sao param thứ 2 lại 0 nhỉ ? Comment ở cuối bài về ý kiến & mức độ hiểu của bạn cho mình ở cuối bài viết nhé. Hãy nhớ rằng chỉ khi bạn viết ra được những ý hiểu của bạn thì bạn mới có thể tiến xa được trong việc học lập trình. Và mình rất mong bình luận chia sẻ đóng góp của các bạn với bài viết của mình.

2.3 Replace item trong array

  • Ví dụ cuối cùng về công dụng của
    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    5

    splice(start)
    splice(start, deleteCount)
    splice(start, deleteCount, item1)
    splice(start, deleteCount, item1, item2, itemN)

    5

  • Kết quả sẽ xuất hiện khi các bạn code lại trên màn hình console của các bạn. Ví này thoạt nhìn qua cũng khá giống với ví dụ
    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    6 mình đã nêu trên, nhưng hãy nhớ đấy là

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    9. Vậy

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    9 là việc chúng ta xoá bỏ đi các item và thay thế các item đã xoá bằng các item khác.

3.Tổng kết lại

  • Với splice:
    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    6,

    <button onclick="myFunction()">Run</button>
    
    <p id="demo"></p>
    
    <script>
        var subject = ["php", "css", "html", "js"];
        document.getElementById("demo").innerHTML = subject;
    
        function myFunction() {
            subject.splice(1, 1, "python", "c#", "ios");
            document.getElementById("demo").innerHTML = subject;
        }
    </script>

    2 và

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    9

  • Kết quả splice: sẽ ảnh hưởng (thay đổi trực tiếp) lên array gốc.
  • <button onclick="myFunction()">Run</button>
    
    <p id="demo"></p>
    
    <script>
        var subject = ["php", "css", "html", "js"];
        document.getElementById("demo").innerHTML = subject;
    
        function myFunction() {
            subject.splice(1, 1, "python", "c#", "ios");
            document.getElementById("demo").innerHTML = subject;
        }
    </script>

    4 để bắt đầu cho việc

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    6,

    <button onclick="myFunction()">Run</button>
    
    <p id="demo"></p>
    
    <script>
        var subject = ["php", "css", "html", "js"];
        document.getElementById("demo").innerHTML = subject;
    
        function myFunction() {
            subject.splice(1, 1, "python", "c#", "ios");
            document.getElementById("demo").innerHTML = subject;
        }
    </script>

    2 và

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    9 luôn là param thứ nhất chúng ta truyền vào, tiếp theo param thứ hai là số lượng item chúng ta muốn

    <button onclick="myFunction()">Run</button>
    
    <p id="demo"></p>
    
    <script>
        var subject = ["php", "css", "html", "js"];
        document.getElementById("demo").innerHTML = subject;
    
        function myFunction() {
            subject.splice(1, 1, "python", "c#", "ios");
            document.getElementById("demo").innerHTML = subject;
        }
    </script>

    2 (với bài toán ở ví dụ đầu tiên thì việc truyền param thứ hai vào thì mặc định Javascript sẽ remove số item còn lại). Các param từ thứ 3 trở đi dành cho các item khi chúng ta

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    9 &

    let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon']
    let removed = myFish.splice(2, 2)
    
    console.log(myFish )
    // Kết quả:["parrot", "anemone", "sturgeon"]
    
    console.log(removed)
    // Kết quả:["blue", "trumpet"]

    6 – không giới hạn kể cả truyền 69 param vẫn ok.