Array.prototype.splice()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015ë 7ìâ©.
splice() ë©ìëë ë°°ì´ì 기존 ìì를 ìì ëë êµì²´íê±°ë ì ìì를 ì¶ê°íì¬ ë°°ì´ì ë´ì©ì ë³ê²½í©ëë¤.
ìëí´ ë³´ê¸°
const months = ["Jan", "March", "April", "June"];
months.splice(1, 0, "Feb");
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, "May");
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]
구문
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
매ê°ë³ì
start-
ë°°ì´ì ë³ê²½ì ììí ì¸ë±ì¤ì ëë¤. ë°°ì´ì 길ì´ë³´ë¤ í° ê°ì´ë¼ë©´ ì¤ì ìì ì¸ë±ì¤ë ë°°ì´ì 길ì´ë¡ ì¤ì ë©ëë¤. ììì¸ ê²½ì° ë°°ì´ì ëììë¶í° ìì를 ì¸ì´ëê°ëë¤(ìì -1, ì¦ -nì´ë©´ ìì ëì në²ì§¸ ìì를 ê°ë¦¬í¤ë©°
array.length - në²ì§¸ ì¸ë±ì¤ì ê°ì). ê°ì ì ëê°ì´ ë°°ì´ì ê¸¸ì´ ë³´ë¤ í° ê²½ì° 0ì¼ë¡ ì¤ì ë©ëë¤. deleteCountOptional-
ë°°ì´ìì ì ê±°í ììì ìì ëë¤.
deleteCount를 ìëµíê±°ë ê°ì´array.length - startë³´ë¤ í¬ë©´startë¶í°ì 모ë ìì를 ì ê±°í©ëë¤.deleteCountê° 0 ì´íë¼ë©´ ì´ë¤ ììë ì ê±°íì§ ììµëë¤. ì´ ëë ìµìí íëì ìë¡ì´ ìì를 ì§ì í´ì¼ í©ëë¤. item1, item2, <em>...</em>Optional-
ë°°ì´ì ì¶ê°í ììì ëë¤. ì무 ììë ì§ì íì§ ìì¼ë©´
splice()ë ìì를 ì ê±°íê¸°ë§ í©ëë¤.
ë°í ê°
ì ê±°í ìì를 ë´ì ë°°ì´. íëì ììë§ ì ê±°í ê²½ì° ê¸¸ì´ê° 1ì¸ ë°°ì´ì ë°íí©ëë¤. ì무 ê°ë ì ê±°íì§ ììì¼ë©´ ë¹ ë°°ì´ì ë°íí©ëë¤.
ì¤ëª
ë§ì½ ì ê±°í ììì ìì ì¶ê°í ììì ìê° ë¤ë¥¸ ê²½ì° ë°°ì´ì 길ì´ë ë¬ë¼ì§ëë¤.
ìì
>íëë ì ê±°íì§ ìê³ , 2ë² ì¸ë±ì¤ì "drum" ì¶ê°
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(2, 0, "drum");
// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
íëë ì ê±°íì§ ìê³ , 2ë² ì¸ë±ì¤ì "drum"ê³¼ "guitar" ì¶ê°
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(2, 0, "drum", "guitar");
// myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// removed is [], no elements removed
3ë² ì¸ë±ì¤ìì í ê° ìì ì ê±°
var myFish = ["angel", "clown", "drum", "mandarin", "sturgeon"];
var removed = myFish.splice(3, 1);
// removed is ["mandarin"]
// myFish is ["angel", "clown", "drum", "sturgeon"]
2ë² ì¸ë±ì¤ìì í ê° ìì ì ê±°íê³ "trumpet" ì¶ê°
var myFish = ["angel", "clown", "drum", "sturgeon"];
var removed = myFish.splice(2, 1, "trumpet");
// myFish is ["angel", "clown", "trumpet", "sturgeon"]
// removed is ["drum"]
0ë² ì¸ë±ì¤ìì ë ê° ìì ì ê±°íê³ "parrot", "anemone", "blue" ì¶ê°
var myFish = ["angel", "clown", "trumpet", "sturgeon"];
var removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
// myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed is ["angel", "clown"]
2ë² ì¸ë±ì¤ìì ë ê° ìì ì ê±°
var myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];
var removed = myFish.splice(myFish.length - 3, 2);
// myFish is ["parrot", "anemone", "sturgeon"]
// removed is ["blue", "trumpet"]
-2ë² ì¸ë±ì¤ìì í ê° ìì ì ê±°
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(-2, 1);
// myFish is ["angel", "clown", "sturgeon"]
// removed is ["mandarin"]
2ë² ì¸ë±ì¤ë¥¼ í¬í¨í´ì ì´íì 모ë ìì ì ê±°
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(2);
// myFish is ["angel", "clown"]
// removed is ["mandarin", "sturgeon"]
ëª ì¸
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-array.prototype.splice > |