Javascript Array object (Array object)

Source: Internet
Author: User

1. method for creating an Array object:
---> Var arr = [1, 2, 3]; // simple definition method
Now you can know
Arr [0] = 1;
Arr [1] = 2;
Arr [2] = 3;
---> New Array ();
Var arr = new Array (); // defines an Array object without any content, and assigns a value to it in the following way:
Arr [0] = "arr0 ";
Arr [1] = "arr1 ";
Arr [2] = "arr2 ";
---> New Array (size); // defines an Array object with limited size, and assigns values in the following way (the assignment method is the same as above)
Var arr = new Array (3 );
Arr [0] = "arr0 ";
Arr [1] = "arr1 ";
Arr [2] = "arr2 ";
---> New Array (element0, element1,..., elementn); // define the Array content directly
Var arr = new Array ("arr0", "arr1", "arr2 ");
Now you can know:
Arr [0] = "arr0 ";
Arr [1] = "arr1 ";
Arr [2] = "arr2 ";
2. Array Object Attributes
Array has three common attributes: constructor, length, and prototype.
---> Constructor, as its name implies, is a constructor, that is, what the object is composed of. In other words, it is the object type. See the following example.
Var arr = new Array (3 );
If (arr. constructor = Array)
{
Document. write ("This is an Array ");
}
If (test. constructor = Boolean)
{
Document. write ("This is a Boolean ");
}
If (test. constructor = Date)
{
Document. write ("This is a Date ");
}
If (test. constructor = String)
{
Document. write ("This is a String ");
}
The output above is: This is an Array
---> Length, that is, the length of Array
Var arr = new Array (3 );
Document. write (arr. length); // The output result is 3.
Note: In Javascript, you can modify the attributes of an Array object,
Therefore:
Arr. length = 5;
Document. write (arr. length); // The output result is 5.
---> Prototype enables you to add attributes and methods to an object.
Function myarray (name, age) // defines a class, which currently has two attributes
{
This. name = name;
This. age = age;
}
Var myarr = new myarray ("john", 25 );
Myarray. prototype. test = null; // added an attribute for the myarray class
Myarr. test = "test ";
Alert (myarr. test); // output test
3. concat () method ---> concatenate two or more Arrays
It can be used in two ways:
---> Connect to the actual data
Example:
Var arr = new Array (1, 2, 3 );
Alert (arr. concat (); // output, 5
---> Connect two or more Arrays
Var arr1 = new Array (1, 2, 3 );
Var arr2 = [4, 5];
Var arr3 = new Array ("jone", "john ");
Alert (arr1.concat (arr2, arr3); // output 1, 2, 3, 4, 5, jone, john
4. join () method ---> put the elements in the array into a string
It can have parameters or no parameters. The parameters represent the method of separating the generated string.
---> NO Parameter
Var arr = new Array ("jone", "Grrgy", "john ");
Alert (arr. join (); // outputs jone, Grrgy, and john strings separated by commas (,).
---> Parameters
Var arr = new Array ("jone", "Grrgy", "john ");
Alert (arr. join ("."); // output the jone. Grrgy. john string separated by Parameters
5. the pop () method is used to delete and return the last element of the array (before deletion)
Var arr = new Array ("jone", "john", "grrgy ");
Document. write (arr. pop (); // output content: grrgy
Document. write (arr. join ("-"); // output: jone-john
6. The push () method is used to add an element to the last array and return the length of the array (after adding)
If the parameter in push () is null (not filled), the original length of the array is returned without any modification to the array.
Example:
Var arr = ["jone", "john", "grrgy"];
Document. write (arr. push ("tom"); // output: 4 (length)
Document. write (arr. join (); // output: jone, john, grrgy, tom
7. reverse the order of elements in the array with no parameters
Example:
Var arr = ["jone", "john", "grrgy"];
Document. write (arr. reverse (); // grrgy, john, jone
8. shift () Delete and return the first element of the array (before deletion)
Var arr = ["jone", "john", "grrgy"];
Document. write (arr. shift (); // output: jone
Document. write (arr. join (); // output: jone, john
9. slice () returns the specified element from the specified array. Note: It returns an array.
Its Parameters include start and end,
Start is required. It specifies the position of the start Element.
End is optional. It specifies the position of the end element. If it is not written, it is considered to end with the array.
Example:
Var arr = ["jone", "john", "grrgy", "tom", "hell"];
Var test = arr. slice (1 );
If (test. constructor = Array)
{
Document. write ("This is an Array <br> ");
Document. write (test. join ());
}
Final result output:
This is an Array
John, grrgy, tom, hell
If you change var test = arr. slice (1):
Var test = arr. slice (1, 2 );
Result output:
John
10. sort () is an important method for sorting array elements.
It can have parameters. The parameter is a function (), which specifies the sorting rules,
Note: It generates a copy of the original array and does not generate a new array, that is, it is modified on the basis of the original array.
If no parameter is added, it is sorted alphabetically by the built-in sorting method in Javascript.
Example:
Var arr = ["jone", "john", "grrgy", "tom", "hell"];
Document. write (arr. sort ());
Document. write ("<br> ");
Document. write (arr );
Output result:
Grrgy, hell, john, jone, tom
Grrgy, hell, john, jone, tom
Sort by number
Function sortNumber (a, B) // function used to define the sorting rule
{
If (a> B)
{
Return 1;
}
Else if (a <B)
{
Return-1;
}
Else
{
Return 0;
}

}
Var arr = new Array (3,400 );
Document. write (arr. sort (sortNumber); // write only the function name here.
Document. write ("<br> ");
Document. write (arr );
Output:


11. splice () deletes an element and adds it to the array.
Splice (index, howmany, element1, element2.... elementx) is described as follows:
Index is required to specify where to add/delete elements. This parameter is the subscript of the array element that begins to insert or delete. It must be a number.
Howmany is required. Specifies how many elements should be deleted. It must be a number, but it can be "0 ". If this parameter is not specified, all elements starting from index to the end of the original array are deleted.
When howmany is 0, it indicates that no elements are deleted. The implication is to add only
Element1 is optional and specifies the new elements to be added to the array. Insert from the subscript indicated by index. you can insert multiple
The difference between splice () and slice () Is that splice () processes the original array. It modifies the value of the original array and returns an array.
Splice () is equivalent to replacing or inserting or deleting an element in the array.
Let's look at the following three examples:
---> Insert only
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/> ");
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, William, Thomas, James, Adrew, Martin
William inserted to 2
---> Delete only
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 );
Document. write (arr + "<br/> ");
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, James, Adrew, Martin
The element at the position of array 2 is deleted.
---> Delete and add (equivalent to replacement)
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 + "<br/> ");
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, William, James, Adrew, Martin
Replace the original Thomas with William.
12. toSource () returns the source code of the object. This method is generally automatically called in the background of Javascript and rarely used in the foreground.
This method cannot be implemented in IE browser, for example, in firefox
Var myarr = new Array ('lisi', 25 );
Document. write (myarr. toSource ());
Output result:
["Lisi", 25]
If a new class is defined, the attribute name can be displayed, for example:
Function myarray (name, age)
{
This. name = name;
This. age = age;
}
Var myarr = new myarray ('lisi', 25 );
Document. write (myarr. toSource ());
Output result:
({Name: "lisi", age: 25 })
It is similar to Json data, but it is only similar. It is not a Json Data Type format.
13. toString (). The array is returned as a string. It returns the same result as the join () method. However, the join () method can customize the delimiter.
ToString () is not allowed. It can only be separated by commas (,). For example:
Var myarr = new Array ('jone', 'john', 'Tom ');
Document. write (myarr. join ('.'));
Document. write ('<br> ');
Document. write (myarr. join (','));
Document. write ('<br> ');
Document. write (myarr. join ());
Document. write ('<br> ');
Document. write (myarr. toString ());
Output result:
Jone. john. Tom
Jone, john, Tom
Jone, john, Tom
Jone, john, Tom www.2cto.com
The results of the last three methods are the same.
14. unshift (). You can add one or more elements to the beginning of the array and return the new length of the array. The original array will change.
Unshift (element1, element2, element3....) has at least one element, for example:
Var myarr = new Array ('jone', 'john', 'Tom ');
Var length = myarr. unshift ('hangsan', 'lisi ');
Document. write (myarr );
Document. write ('<br> ');
Document. write (length );
Output result:
Zhangsan, lisi, jone, john, Tom

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.