Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the splice () function of JavaScript

Shulou Source: shulou.com Published: 2022-05-31 20:09:16 09月29日 Update

This article mainly explains "how to use the splice () function of JavaScript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the splice () function of JavaScript.

Detailed explanation of splice () function

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

Note: this method changes the original array.

Parameters:

Index-required. An integer that specifies the location of the added / deleted items, using negative numbers to specify the location from the end of the array. Howmany-required. The number of items to delete. If set to 0, the item is not deleted. Item1,... , itemX-optional. A new item added to the array.

Return value

Array-contains a new array of deleted items, if any. Case 1 (only one parameter)

Splice (index)-- > start with the location of the index and delete all subsequent elements (including the index)

If index

< 0 , 则删除最后-index个元素 splice()函数返回删除元素数组 var array = [0,1,2,3,4,5,6] [xss_clean]("原数组为:"+array + "") array.splice(2) [xss_clean]("array.splice(2)的结果为:",array)

Var array = [xss_clean] ("original array is" + array + ") var result = array.splice (- 3) [xss_clean] (" array.splice (- 3) result is: ", array +") [xss_clean] ("deleted content is:", result)

Case 2 (two parameters)

Splice (index,howmany)-- > the number of deletions starting from the index location. Howmany is the number of deletions.

If howmany is less than or equal to 0, it will not be deleted

Var array = [xss_clean] ("original array is" + array + ") var result = array.splice (2) [xss_clean] (" array.splice (2)) the result is: ", array +") [xss_clean] ("deleted content is:", result)

Var result = array.splice (2jin0) [xss_clean] ("the result of array.splice (2jin0) is:", array + ") [xss_clean] (" deleted content is: ", result)

Case 3 (greater than or equal to three parameters)

Splice (index, howmany, item1, … , itemX)

When index > 0

(1. Do not delete and only add when howmany is 0-- add item1 before the index location. , the number of itemX

(2. Howmany > 0 delete and add-add item1 before index location, … , the number of itemX, and delete the number starting from the index position, howmany is the number of deletions index 0 delete and add-- add item1 before the-index position. , the number of itemX, and delete the number starting from the-index position, howmany is the number of deletions

1.1

Var array = [xss_clean] ("original array is:" + array + ") var result = array.splice (" 2memo 8je 9) [xss_clean] ("array.splice") the result is: ", array +") [xss_clean] ("deleted content is:", result)

1.2

Var result = array.splice [xss_clean] ("the result of array.splice is:", array + ") [xss_clean] (" deleted content is: ", result)

2.1

Var array = [xss_clean] ("the original array is:" + array + ") var result = array.splice (- 2meme) [xss_clean] (" the result of array.splice ("array.splice (- 2Power8)) is:", array + ") [xss_clean] (" deleted content is: ", result)

2.2

Var array = [xss_clean] ("the original array is:" + array + ") var result = array.splice (- 2meme 3d8) [xss_clean] (" the result of array.splice ("array.splice (- 2d3)) is:", array + ") [xss_clean] (" deleted content is: ", result)

At this point, I believe you have a deeper understanding of "how to use the splice () function of JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array location content result project function number parameter element case method learning original practical deeper three two interest only practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Apple MariaDB MySQL