JavaScript Массив splice()
Examples
// Создаем массив
const fruits = ["Банан", "Апельсин", "Яблоко", "Манго"];
// На позиции 2 добавляем "Лимон" и "Киви":
fruits.splice(2, 0, "Лимон", "Киви");
Try it Yourself »
More Examples Below !
Description
The splice()
method adds and/or removes array elements.
The splice()
method overwrites the original array.
Syntax
array.splice(index, count, item1, ....., itemX)
Parameters
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. |
Return Value
Type | Description |
Array | An array containing the removed items (if any). |
More Examples
Example
// Создаем массив
const fruits = ["Банан", "Апельсин", "Яблоко", "Манго"];
// На позиции 2 удалите 2 элемента
fruits.splice(2, 2);
Try it Yourself »
Example
// Создаем массив
const fruits = ["Банан", "Апельсин", "Яблоко", "Манго"];
// На позиции 2 удаляем 1 элемент, добавляем "Лимон" и "Киви"
fruits.splice(2, 1, "Лимон", "Киви");
Try it Yourself »
Array Tutorials:
Browser Support
splice()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |