[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/splice [Back]  [Original]

Array.prototype.splice() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.splice()

Baseline

20157

splice() Array (in-place)

toSpliced() slice()

const months = ["Jan", "March", "April", "June"];
months.splice(1, 0, "Feb");
//  1 
console.log(months);
// : Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, "May");
//  4  1 
console.log(months);
// : Array ["Jan", "Feb", "March", "April", "May"]

js
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2)
splice(start, deleteCount, item1, item2, /* , */ itemN)

start

  • start < 0 start + array.length
  • start < -array.length 0
  • start >= array.length
  • start splice() undefined 0
deleteCount

start

deleteCount deleteCount start start itemN start deleteCount Infinity undefined 0

deleteCount 0 1

item1, , itemN

start

splice()

1 1

splice() this length [Symbol.species]

splice()

splice() this length

2 0 "drum"

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish  ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed  [] 

2 0 "drum" "guitar"

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum", "guitar");

// myFish  ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// removed  [] 

0 0 "angel"

splice(0, 0, ...elements) unshift()

js
const myFish = ["clown", "mandarin", "sturgeon"];
const removed = myFish.splice(0, 0, "angel");

// myFish  ["angel", "clown", "mandarin", "sturgeon"]
// 

0"sturgeon"

splice(array.length, 0, ...elements) push()

js
const myFish = ["angel", "clown", "mandarin"];
const removed = myFish.splice(myFish.length, 0, "sturgeon");

// myFish  ["angel", "clown", "mandarin", "sturgeon"]
// 

3 1

js
const myFish = ["angel", "clown", "drum", "mandarin", "sturgeon"];
const removed = myFish.splice(3, 1);

// myFish  ["angel", "clown", "drum", "sturgeon"]
// removed  ["mandarin"]

2 1 "trumpet"

js
const myFish = ["angel", "clown", "drum", "sturgeon"];
const removed = myFish.splice(2, 1, "trumpet");

// myFish  ["angel", "clown", "trumpet", "sturgeon"]
// removed  ["drum"]

0 2 "parrot" "anemone" "blue"

js
const myFish = ["angel", "clown", "trumpet", "sturgeon"];
const removed = myFish.splice(0, 2, "parrot", "anemone", "blue");

// myFish  ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed  ["angel", "clown"]

2 2

js
const myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];
const removed = myFish.splice(2, 2);

// myFish  ["parrot", "anemone", "sturgeon"]
// removed  ["blue", "trumpet"]

-2 1

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(-2, 1);

// myFish  ["angel", "clown", "sturgeon"]
// removed  ["mandarin"]</pre>

2

js
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2);

// myFish  ["angel", "clown"]
// removed  ["mandarin", "sturgeon"]

splice()

splice()

js
const arr = [1, , 3, 4, , 6];
console.log(arr.splice(1, 2)); // [empty, 3]
console.log(arr); // [1, 4, empty, 6]

splice()

splice() this length length

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  0: 5,
  2: 4,
};
console.log(Array.prototype.splice.call(arrayLike, 0, 1, 2, 3));
// [ 5 ]
console.log(arrayLike);
// { '0': 2, '1': 3, '3': 4, length: 4, unrelated: 'foo' }

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.splice


Web Proxy Viewer  |  New URL  |  Original Page