The use of splice in JS

Source: Internet
Author: User

First, splice grammar

Arrayobject.splice (start, deletecount,item1,....., ItemX)

1. Parameter description

start: Required. Specifies where to add/remove elements from. This parameter is the subscript that begins inserting and/or deleting an array element, which must be a number.

deletecount: Required. Specifies how many elements should be deleted. Must be a number, but it can be "0". If this parameter is not specified, all elements from index start to the end of the original array are deleted.

item1: Optional. Specifies the new element to be added to the array. Start the insertion from the subscript at index point.
ItemX : Optional. You can add several elements to an array.

2. Attention

1, the argument is negative, if start is negative, the length of the array is added as the value of start , and DeleteCount is negative or 0 will not perform the delete operation.

2. Using splice to delete the values in the array will directly remove a few items from the array, causing the array to change the length value, which is not the same as deleting the delete to undefined.

3 . The Splice method modifies the arrayobjby removing the specified number of elements from the start position and inserting the new element. The return value is a new Array object that consists of the elements that were removed.

Ii. Examples of Splice


var lang = ["PHP", "Java", "JavaScript"]; Remove var removed = Lang.splice (n); alert (lang); Php,javascript alert (removed); Java, return Deleted items//insert var insert = Lang.splice (0,0, "ASP"); Insert alert (insert) starting from the No. 0 position; Returns an empty array, alert (lang); Asp,php,javascript//replace var replace = Lang.splice ("C #", "Ruby"); Delete one item and insert two alert (lang); Asp,c#,ruby alert (replace); PHP, returning Deleted Items
var arr = new Array (6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" do Cument.write (arr + "<br/>") Arr.splice (2,0, "William") document.write (arr + "<br/>")//output://george,john, Thomas,james,adrew,martin//george,john,william,thomas,james,adrew,martin


Reference: JS in Splice method http://www.studyofnet.com/news/937.html


The use of splice in JS

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.