Array object parsing for JavaScript

Source: Internet
Author: User
Tags spl

1 One-dimensional arrays

Definition: A one-dimensional array is a set of related data with the same type, and each data item is marked with a single variable name with an index number.

2 Creating an array in JavaScript

in the in JavaScript, use the var keyword followed by the array name to create an array. However, instead of placing a specific value on the right side of the expression, use the JavaScript keyword new Array (). The number of elements in this array can be specified within the parentheses, but this is not required. such as:var array = new Array ($); the method that references a particular element is the array name followed by an index number enclosed in parentheses (numbered from 0 start). such as:document.write (array[2]);

3 Initialization of arrays

The two methods of assignment: 1) First declare and then assign each value individually. such as var stu = new Array (); stu[0]= "* *"; stu[1]= "# #";

2) Declare the assignment at the same time. such as var stu = new Array ("* *", "$$", "&&", "....");

4 Array Object

Because arrays are objects, they have related methods and properties, and when the array name is passed the most arguments, it is a reference pass (that is, the address is passed).

5 a description of the array name

When an array is referenced by an array name, it can be either with or without parentheses.

6 The length property of the array

Check the length size of the array. (array name. length)

7 Some methods of the Array object

Methods for Array objects

Method

Describe

Concat ()

Links two or more arrays and returns a copy of the concatenated array

Join ()

Concatenate all elements of an array into a single string

Push ()

Adds a new element to the end of the array and returns the new length

Reverse ()

Reverses the order of elements in an array

Shift ()

Removes the first element of the array and returns that element

Sort ()

Sort the elements in an array

Splice ()

Add / remove elements to an array

ToString ()

Converts an array to a string and returns the result

Unshift ()

Adds a new element to the beginning of the array and returns the new length

8 Array Method instances

  

<!doctype html>
<meta charset= "Utf-8" >
<title> Untitled Document </title>
<script type= "Text/javascript" >
function Arraymethod () {
var array1 = new Array (3, "Huang", "$");
var array2 = new Array ();
Array2[0] = 5;
Array2[1] = 49;
ARRAY2[2] = 8;
/* Link two or more arrays and return a copy of the connection array: 3,huang,$,5,49,8*/
var con = array1.concat (array2);
document.getElementById ("Concat"). InnerHTML = con;
/* Concatenate all the elements in an array into a single string: 3,huang,$*/
var Joi = Array1.join ();
document.getElementById ("join"). InnerHTML = Joi;
/* Adds a new element to the end of the array and returns the new length: 4*/
var pus = Array1.push ("Jinjin");
document.getElementById ("Push"). InnerHTML = pus;
/* Reverses the order of the elements in the array: jinjin,$,huang,3*/
var rever = Array1.reverse ();
document.getElementById ("reverse"). InnerHTML = Rever;
/* Remove the first element of the array and return that element: jinjin*/
var shif = Array1.shift ();
document.getElementById ("Shift"). InnerHTML = Shif;

/*sort ():
1. By default, it is sorted in ascending order. From left to right a comparison, until the size (if the number of bits of the 0): such as: 5 and 51, because the first digit 5 is equal to 5, compared to the second position, because the first digit is only one bit, so the complement 00 is less than the second digit of the second 1, so 5 is less than 51; function is determined to be 5 greater than 49. So when using this function comparison, the corresponding number of digits should be the same number (as in example 5 and 49 compared, should be written as: 05 and 49 comparison).
2. The sort () method can accept a method as a parameter,
3. Example results: Sorting elements in an array: 49,5,8
*/
var sor = Array2.sort ();
document.getElementById ("Sort"). InnerHTML = sor;
/*
Splice (index,howmany,item1,....., ItemX)
Index: Required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany: Required. The number of items to delete. If set to 0, the item is not deleted.
Item1, ..., ItemX: Optional. Adds a new item to the array.
Note: Splice does not return (that is, no return value)
So the example results are: Add/Remove elements for arrays:
*/
var SPL = Array2.splice (3,0, "4")//
document.getElementById ("Splice"). InnerHTML = SPL;
/* Convert the array to a string and return this result: 49,5,8,4*/
var tos = array2.tostring ();
document.getElementById ("ToString"). InnerHTML = TOS;
/* Add the new element to the beginning of the array and return the new length: 5*/
var unshif = Array2.unshift ("percent")
document.getElementById ("Unshift"). InnerHTML = Unshif;
}

</script>

<body>
<section>
<p> links two or more arrays and returns a copy of the connection array: <span id= "Concat" ></span></p>
<p> joins all elements in an array into a single string: <span id= "Join" ></span></p>
<p> adds a new element to the end of the array and returns a new length: <span id= "Push" ></span></p>
<p> reverses the order of elements in an array: <span id= "Reverse" ></span></p>
<p> removes the first element of the array and returns that element: <span id= "Shift" ></span></p>
<p> sorting elements in an array: <span id= "Sort" ></span></p>
<p> Add/Remove elements to an array: <span id= "Splice" ></span></p>
<p> converts the array to a string and returns the result: <span id= "ToString" ></span></p>
<p> adds a new element to the beginning of the array and returns the new length: <span id= "Unshift" ></span></p>
<input type= "button" value= "Array_method" onclick= "Arraymethod ()" >
</section>
</body>

Attention:

1, Splice () method:(do not return )

2.the Sort() method is sorted by the ASCII code of the character data

Note: I am a new person, if there is inappropriate, please understand the place. Thank you!

Array object parsing for JavaScript

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.