Home>Article>Web Front-end> What is the splice() method in JS?

What is the splice() method in JS?

coldplay.xixi
coldplay.xixi Original
2020-07-02 11:23:12 5413browse

The splice() method adds or removes items from the array and then returns the deleted item. This method will change the original array. The syntax is [arrayObject.splice(index,howmany,item1,.... .,itemX)].

What is the splice() method in JS?

The splice() method in JS is:

1. Definition and usage

splice()The method adds/removes items from the array and then returns the deleted item.

Note: This method will change the original array.

2. Syntax

arrayObject.splice(index,howmany,item1,.....,itemX)

3. Description

  • splice()Method removes zero or more elements starting atindexand replaces those removed elements with one or more values declared in the parameter list.

  • If an element is deleted fromarrayObject, the array containing the deleted element is returned.

5. Example

Example 1

In this example, we will create a new array and add it to Add an element:

Output:

George,John,Thomas,James,Adrew,Martin George,John,William,Thomas,James,Adrew,Martin

Example 2

In this example we will delete the element at index 2 and add a new element to replace the deleted element Elements:

Output:

George,John,Thomas,James,Adrew,Martin George,John,William,James,Adrew,Martin

Related learning recommendations:javascript video tutorial

The above is the detailed content of What is the splice() method in JS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn