The most powerful array method of JavaScript-splice ()

Source: Internet
Author: User

The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted.

The main purpose of the splice () method is to insert items into the middle of the array, using this method in the following ways:

    • Delete: You can delete any number of items, you only need to set 2 parameters: the location of the first item to delete and the number of items to delete.

Example:

var array = new Array ("Niu", "Li", "NA"), var deleteItem = Array.splice (0,2); Console.log ("DeleteItem:" + Deleteitem.tostring ()); Console.log ("Array:" +array.tostring ());

Results:

The result shows that the parameter 1 is 0, which starts with the No. 0 item and then deletes 2 items. Deleted items are returned, and this change affects the original array, so the original array has only the item "NA" left.

    • Insert: You can insert any number of items into a location, providing only three parameters: Start position, 0 (number of items to delete), and items to insert. If you want to insert more than one item, you can pass in four, five, so that any number of items.

Example:

var array = new Array ("Niu", "Li", "NA"), var deleteItem = Array.splice (0,0, "Da", "Jia", "Hao"); Console.log ("DeleteItem:" + Deleteitem.tostring ()); Console.log ("Array:" +array.tostring ());

Results:

The function returned is still the deleted item, and the new item was inserted in the deleted position.

    • Replace: You can insert any number of items at the specified location and delete any number of items at the same time by specifying three parameters: The starting position, the number of items to delete, and any number of items to insert, the number of items to be inserted does not have to be the same as the number of deletions.

Example:

var array = new Array ("Niu", "Li", "NA"), var deleteItem = Array.splice (0,1, "Da", "Jia", "Hao"); Console.log ("DeleteItem:" + Deleteitem.tostring ()); Console.log ("Array:" +array.tostring ());

Results:

  

The function deletes an item on the basis of the original array, and the function returns the deleted item, and the new item is inserted in the deleted position.

The difference between the Insert function and the Replace function is parameter 2.

  

The most powerful array method of JavaScript-splice ()

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.