Splice method usage in JavaScript _javascript tips

Source: Internet
Author: User

Splice in JavaScript is primarily used to manipulate arrays in JS, including deletions, additions, replacements, and so on.

Note: This method will change the original array!.

1. Delete-To delete the element, two parameters, the first argument (to delete the position of the first item), the second argument (number of items to delete)

2. Insert-Inserts any item element into the array at the specified position. Three parameters, first argument (insertion position), second argument (0), third argument (inserted item)

3. Replace-inserts any item element into the array at the specified position, deleting any number of items, and three parameters. First argument (starting position), second argument (number of deleted items), third argument (inserts any number of items)

Example:

1, delete function, the first parameter is the first position, the second parameter is to delete a few.

Array.splice (Index,num), the return value is deleted, and array is the result value.

eg

<! DOCTYPE html> 
 
 

2. Insert function, first parameter (insertion position), second parameter (0), third parameter (inserted item)

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

eg

<! DOCTYPE html> 
 
 

3. Replace function, first parameter (starting position), second parameter (number of deleted items), third parameter (insert any number of items)

Array.splice (Index,num,insertvalue), the return value is deleted, and array is the result value.

eg

<! DOCTYPE html> 
 
 

The above is a small set to introduce the JavaScript in the Splice method usage detailed, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.