Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the use of the splice () method in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 13:06:48 09月16日 Update

This article mainly introduces the relevant knowledge of "what is the use of the splice () method in JavaScript". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "what is the use of the splice () method in JavaScript" can help you solve the problem.

Definition and usage

The splice () method is used to insert, delete, or replace elements of an array.

Syntax arrayObject.splice (index,howmany,element1,.,elementX) parameter describes index

Necessary. Specifies where to add / remove elements.

This parameter is the subscript of the array element that starts to be inserted and / or deleted, and must be a number.

Howmany

Necessary. Specifies how many elements should be deleted. Must be a number, but can be "0".

If this parameter is not specified, all elements from the beginning of index to the end of the original array are deleted.

Element1 is optional. Specifies the new elements to be added to the array. Start the insertion at the subscript referred to by index. ElementX is optional. Several elements can be added to the array. Return value

If you delete an element from arrayObject, an array containing the deleted element is returned.

Description

The splice () method deletes zero or more elements starting at index and replaces those deleted elements with one or more values declared in the parameter list.

Tips and comments

Note: note that the function of the splice () method is different from that of the slice () method, which modifies the array directly.

Example 1

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

Var arr = new Array (6) arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" arr [3] = "James" arr [4] = "Adrew" arr [5] = "Martin" [xss_clean] (arr + ") arr.splice (2) 0," William ") [xss_clean] (arr +")

Output:

George,John,Thomas,James,Adrew,MartinGeorge,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:

Var arr = new Array (6) arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" arr [3] = "James" arr [4] = "Adrew" arr [5] = "Martin" [xss_clean] (arr + ") arr.splice (2) 1," William ") [xss_clean] (arr)

Output:

George,John,Thomas,James,Adrew,MartinGeorge,John,William,James,Adrew,Martin example 3

In this example, we will delete the three elements starting with index 2 ("Thomas") and add a new element ("William") to replace the deleted element:

Var arr = new Array (6) arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" arr [3] = "James" arr [4] = "Adrew" arr [5] = "Martin" [xss_clean] (arr + ") arr.splice (2Magol 3," William ") [xss_clean] (arr)

Output:

This is the end of George,John,Thomas,James,Adrew,MartinGeorge,John,William,Martin 's introduction to "what's the use of the splice () method in JavaScript?" Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Elements methods arrays parameters examples knowledge output different subscript multiple numbers comments industry utility three functions content examples practicality practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Microsoft MySQL Huawei