| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/splice | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.
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[, ...]]]])
start0 -1 0
deleteCount deleteCount array.length - start deleteCount start start deleteCount 0
item1, item2, ... start splice() start deleteCount
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(2, 0, "drum");
// myFish ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed [],
var myFish = ["angel", "clown", "drum", "mandarin", "sturgeon"];
var removed = myFish.splice(3, 1);
// removed ["mandarin"]
// myFish ["angel", "clown", "drum", "sturgeon"]
var myFish = ["angel", "clown", "drum", "sturgeon"];
var removed = myFish.splice(2, 1, "trumpet");
// myFish ["angel", "clown", "trumpet", "sturgeon"]
// removed ["drum"]
var myFish = ["angel", "clown", "trumpet", "sturgeon"];
var removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
// myFish ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed ["angel", "clown"]
var myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];
var removed = myFish.splice(myFish.length - 3, 2);
// myFish ["parrot", "anemone", "sturgeon"]
// removed ["blue", "trumpet"]
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(-2, 1);
// myFish ["angel", "clown", "sturgeon"]
// removed ["mandarin"]
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var removed = myFish.splice(2);
// myFish ["angel", "clown"]
// removed ["mandarin", "sturgeon"]
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.splice |
push() / pop() add/remove elements from the end of the arrayunshift() / shift() add/remove elements from the beginning of the arrayconcat() returns a new array comprised of this array joined with other array(s) and/or value(s)This page was last modified on 2025714 by MDN contributors.
Arrayat()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()flatMap()forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()some()Array.prototype.sort()Array.prototype.splice()toLocaleString()toReversed()toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |