JS in the array Insert character implementation code (can refer to JavaScript splice () method) _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

Array.prototype.arrayinsertafter=function (Num,obj)
{
var temparr=new Array ();
var l=this.length;
for (Var i=0;i<l;i++)
{
Temparr.push (This.shift ());
}
L=temparr.length;
for (Var i=0;i<l;i++)
{
This.push (Temparr.shift ());
if (i==num)
{
This.push (obj);
}
}
return this;
}

JavaScript Splice () Method Usage Description
Definitions and usage
The splice () method inserts, deletes, or replaces the elements of an array.

Grammar
Arrayobject.splice (index,howmany,element1,....., elementx)
Parameters Description
Index

Necessary. Specify where to add/remove elements from.

This parameter is the subscript for the array element that starts inserting and/or deleting, and must be a number.

Howmany

Necessary. Specify how many elements should be deleted. Must be a number, but it can be "0".

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

Element1 Optional. Specify the new element to add to the array. Start the insertion at the lower mark that is indicated by index.
Elementx Optional. You can add several elements to an array.
return value
If an element is removed from the Arrayobject, an array containing the deleted elements is returned.
description
The splice () method deletes 0 or more elements starting at index, and replaces the deleted elements with one or more values declared in the argument list.
Tips and Comments
Note: Notice that the splice () method is different from the slice () method, and the splice () method is modified directly to the array.
instance
Example 1
In this case, we'll 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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Output:

George,john,thomas,james,adrew,martin
George,john,william,thomas,james,adrew,martin

Example 2
In this example we will delete the element at 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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.