Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.toSpliced()

Baseline2023
Newly available

ThetoSpliced() method ofArray instances is thecopying version of thesplice() method. It returns a new array with some elements removed and/or replaced at a given index.

Syntax

js
toSpliced(start)toSpliced(start, skipCount)toSpliced(start, skipCount, item1)toSpliced(start, skipCount, item1, item2)toSpliced(start, skipCount, item1, item2, /* …, */ itemN)

Parameters

start

Zero-based index at which to start changing the array,converted to an integer.

  • Negative index counts back from the end of the array — if-array.length <= start < 0,start + array.length is used.
  • Ifstart < -array.length orstart is omitted,0 is used.
  • Ifstart >= array.length, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided.
skipCountOptional

An integer indicating the number of elements in the array to remove (or, to skip) fromstart.

IfskipCount is omitted, or if its value is greater than or equal to the number of elements after the position specified bystart, then all the elements fromstart to the end of the array will be deleted. However, if you wish to pass anyitemN parameter, you should passInfinity asskipCount to delete all elements afterstart, because an explicitundefined getsconverted to0.

IfskipCount is0 or negative, no elements are removed.In this case, you should specify at least one new element (see below).

item1, …,itemNOptional

The elements to add to the array, beginning fromstart.

If you do not specify any elements,toSpliced() will only remove elements from the array.

Return value

A new array that consists of all elements beforestart,item1,item2, …,itemN, and all elements afterstart + skipCount.

Description

ThetoSpliced() method, likesplice(), does multiple things at once: it removes the given number of elements from the array, starting at a given index, and then inserts the given elements at the same index. However, it returns a new array instead of modifying the original array. The deleted elements therefore are not returned from this method, but they remain accessible in the original array.

ThetoSpliced() method never produces asparse array. If the source array is sparse, the empty slots will be replaced withundefined in the new array.

ThetoSpliced() method isgeneric. It only expects thethis value to have alength property and integer-keyed properties.

Examples

Deleting, adding, and replacing elements

You can usetoSpliced() to delete, add, and replace elements in an array and create a new array more efficiently than usingslice() andconcat().

js
const months = ["Jan", "Mar", "Apr", "May"];// Inserting an element at index 1const months2 = months.toSpliced(1, 0, "Feb");console.log(months2); // ["Jan", "Feb", "Mar", "Apr", "May"]// Deleting two elements starting from index 2const months3 = months2.toSpliced(2, 2);console.log(months3); // ["Jan", "Feb", "May"]// Replacing one element at index 1 with two new elementsconst months4 = months3.toSpliced(1, 1, "Feb", "Mar");console.log(months4); // ["Jan", "Feb", "Mar", "May"]// Original array is not modifiedconsole.log(months); // ["Jan", "Mar", "Apr", "May"]

Using toSpliced() on sparse arrays

ThetoSpliced() method always creates a dense array.

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

Calling toSpliced() on non-array objects

ThetoSpliced() method reads thelength property ofthis. It then reads the integer-keyed properties needed and writes them into the new array.

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

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.tospliced

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp