Mối nối mảng JavaScript()
Ví dụ
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// At position 2, add "Lemon" and "Kiwi":
fruits.splice(2, 0, "Lemon", "Kiwi");
Hãy tự mình thử »Thêm ví dụ dưới đây!
Sự miêu tả
Phương thức splice()
thêm và/hoặc xóa các phần tử mảng.
Phương thức splice()
ghi đè lên mảng ban đầu.
Cú pháp
array .splice( index , count , item1 , ....., itemX )
Thông số
Parameter | Description |
index | Required. The index (position) to add or remove items. A negative value counts from the end of the array. |
count | Optional. Number of items to be removed. |
item1 , ..., | Optional. The new elements(s) to be added. |
Giá trị trả về
Type | Description |
Array | An array containing the removed items (if any). |
Thêm ví dụ
Ví dụ
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// At position 2, remove 2 items
fruits.splice(2, 2);
Hãy tự mình thử »Ví dụ
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// At position 2, remove 1 item, add "Lemon" and "Kiwi"
fruits.splice(2, 1, "Lemon", "Kiwi");
Hãy tự mình thử »Hướng dẫn mảng:
Hỗ trợ trình duyệt
splice()
là một tính năng của ECMAScript1 (ES1).
ES1 (JavaScript 1997) được hỗ trợ đầy đủ trên tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |