자바스크립트 배열 splice()
예
위치 2에서 2개의 요소를 추가합니다.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
위치 2에서 2개의 항목을 제거합니다.
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.splice(2, 2);
정의 및 사용
이 splice()
메서드는 배열 요소를 추가 및/또는 제거합니다.
이 splice()
메서드는 원래 배열을 덮어씁니다.
통사론
array.splice(index, howmany, item1, ....., itemX)
매개변수
Parameter | Description |
index | Required. The position to add/remove items. Negative value defines the position from the end of the array. |
howmany | Optional. Number of items to be removed. |
item1, ..., itemX | Optional. New elements(s) to be added. |
반환 값
제거된 항목을 포함하는 배열(있는 경우). |
더 많은 예
위치 2에서 새 항목을 추가하고 1개 항목을 제거합니다.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");
브라우저 지원
splice()
ECMAScript1(ES1) 기능입니다.
ES1(JavaScript 1997)은 모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |