Detailed usage of splice in JavaScript, javascriptsplice

Source: Internet
Author: User

Detailed usage of splice in JavaScript, javascriptsplice

Splice in JavaScript is mainly used to operate arrays in js, including deleting, adding, and replacing arrays.

Note:This method will change the original array !.

1. Delete-used to delete an element. There are two parameters: the first parameter (the location of the first item to be deleted) and the second parameter (number of items to be deleted)

2. Insert-insert any element to the specified position of the array. Three parameters: the first parameter (insert position), the second parameter (0), and the third parameter (insert item)

3. replace-insert any element to the specified position of the array, and delete any number of items, three parameters. The first parameter (start position), the second parameter (number of deleted items), and the third parameter (insert any number of items)

Example:

1. delete function. The first parameter is the first parameter and the second parameter is the number of parameters to be deleted.

Array. splice (index, num). The returned value is the deleted content, and array is the result value.

Eg:

<! DOCTYPE html> <body> <script> var array = ['A', 'B', 'C', 'D']; var removeArray = array. splice (); alert (array); // pop up c, d alert (removeArray); // the return value is the delete item, that is, pop up, B </script> </body> 

2. Insert function: the first parameter (insert position), the second parameter (0), and the third parameter (insert item)

Array. splice (index, 0, insertValue), return value is an empty array, array value is the final result value

Eg:

<! DOCTYPE html> <body> <script> var array = ['A', 'B', 'C', 'D']; var removeArray = array. splice (, 'insert'); alert (array); // Pop Up a, insert, B, c, d alert (removeArray ); // empty </script> </body> 

3. replacement function: the first parameter (start position), the second parameter (number of deleted items), and the third parameter (insert any number of items)

Array. splice (index, num, insertValue). The returned value is the deleted content, and array is the result value.

Eg:

<! DOCTYPE html> <body> <script> var array = ['A', 'B', 'C', 'D']; var removeArray = array. splice (, 'insert'); alert (array); // Pop Up a, insert, c, d alert (removeArray ); // pop up B </script> </body> 

The above is a detailed explanation of the usage of the splice method in JavaScript. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.