JS Add/Remove array element definitions and usage

Source: Internet
Author: User

<script>
var a = new Array (1, 2, 3)
var B = A.push (4,5,[6,7])//a for [1, 2, 3, 4, 5, [6, 7]] B for 6 note the push () method will not open an array for you
var c = new Array (1, 2, 3, 4, "a")
var d = c.pop ()//c is [1, 2, 3, 4] D as a string of "a"
</script>
For more details please see: http://www.111cn.net/wy/js-ajax/34754_1.htm


JS Add/Remove array element definitions and usage
The splice () method inserts, deletes, or replaces the elements of an array.

Grammar
Arrayobject.splice (index,howmany,element1,....., elementx)
Parameter description
Index required. 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 required. 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.


<script>
A=new Array (' A ', ' B ', ' C ');
A.splice (1,1);
for (i=0;i<a.length;i++)
{
Alert (A[i]);
}
</script>

See more detailed examples

<div id= "Example" >

<div id= "Example_main" >


<!--************************************* instance code started *************************************-->

<script language= "JavaScript" >
<!--
Array.prototype.insertat = function (index, value) {
var part1 = this.slice (0, index);
var part2 = this.slice (index);
Part1.push (value);
Return (Part1.concat (part2));
};

Array.prototype.removeat = function (index)
{
var part1 = this.slice (0, index);
var part2 = this.slice (index);
Part1.pop ();
Return (Part1.concat (part2));
}
-->
</script>

<p>
<strong> Original Array </strong>
<br/>
var numbers = new Array (8888, 3561, 0, 4133, 5555, 1); <br/>

<script language= "JavaScript" >
<!--
var numbers = new Array (8888, 3561, 0, 4133, 5555, 62, 1);
-->
</script>

<br/><br/>

<strong> perform the following operations </strong><br/>
Numbers = Numbers.removeat (5); <br/>
Numbers = Numbers.insertat (2, 666); <br/><br/><br

 <strong> results </strong><br/>
 <script language= "javascript"
 < !--
  numbers = numbers.removeat (5);
  numbers = Numbers.insertat (2, 666);
  document.write (Numbers.join (","));
 //
 </script>
</p>

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.