Comments on the Javascript splice () method

Source: Internet
Author: User
ArticleDirectory
    • Syntax
    • Return Value
    • Description
    • Example 1
    • Example 2
    • Example 3

Splice, please note that splice is not a split, although the two looks very similar but not a thing.

Splice and not split can be used for Array Operations.

First, let's talk about split. Split Splits a string based on a certain identifier and returns an array.

Example: var a1 = "a, B, c ";

VaR a2 = a1.split (","); // returns an array ["A", "B", "C"]

The most amazing thing is this application:

VaR a1 = "blogguy ";

VaR a2 = a1.split (""); // returns the array ["B", "L", "O", "g", "g", "U ", "Y"]

After the appetizer is completed, let's take a look at splice. This function is used to insert, delete, or replace elements in an array.

Syntax
 
Arrayobject. splice (index, howmany, element1,..., elementx)

Parameters Description
Index

Required. Specifies where to add/delete elements.

This parameter is the subscript of the array element that begins to insert or delete,Must be a number.

Howmany

Required. Specifies how many elements should be deleted. It must be a number, but it can be "0 ".

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

Element1 Optional. Specifies the new element to be added to the array. Insert from the subscript indicated by index.
Elementx Optional. You can add several elements to the array.

 

Return Value

If an element is deleted from an arrayobject, an array containing the deleted element is returned.

Description

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

 

 

Note:Note that the splice () method and slice () method have different functions. The splice () method directly modifies the array.

 

The following is an example:

 

Example 1

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

 
<SCRIPT type = "text/JavaScript"> var arr = new array (6) arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" arr [3] = "James" arr [4] = "Adrew" arr [5] = "Martin" document. write (ARR + "<br/> ")Arr. splice (2, 0, "William ")Document. Write (ARR + "<br/>") </SCRIPT>

Output:

 
George, John, Thomas, James, Adrew, martingeorge, John, William, Thomas, James, Adrew, Martin
 
 
Example 2

In this example, we will delete the Element Located in index 2 and add a new element to replace the deleted element:

 
<SCRIPT type = "text/JavaScript"> var arr = new array (6) arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" arr [3] = "James" arr [4] = "Adrew" arr [5] = "Martin" document. write (ARR + "<br/> ")Arr. splice (2, 1, "William ")Document. Write (ARR) </SCRIPT>

Output:

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

 
<SCRIPT type = "text/JavaScript"> var arr = new array (6) arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" arr [3] = "James" arr [4] = "Adrew" arr [5] = "Martin" document. write (ARR + "<br/> ")Arr. splice (2, 3, "William ")Document. Write (ARR) </SCRIPT>

Output:

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.