Array objects in Javascript

Source: Internet
Author: User

First, define the array and initialize the method:
Var myArray = new Array (1, 3.1415, "love"); // note that elements in the myArray Array are not only elements of the same data type, but can have an integer or floating point type, string and so on. This fully demonstrates the weakening of the Data Type in javascript as a language. The language is more casual and simple. Just like using var to define an object.
The introduction here is limited. I hope you will try it yourself and see what the results are. [] Is a parameter that can be omitted.

Attribute of Array:
Length: the length of the array object, that is, the number of array elements. Note that the subscript of the first element is 0.
Document. write (myArray. length); // The result is 3.

Array method:
Copy codeThe Code is as follows:
Join (<separator>): concatenates all elements in the array one by one, and places the elements between the elements with separators.
Document. write (myArray. join ("-"); // output: 1-3.1415-love
Document. write (myArray. join (""); // output result: What is it?
Document. write (myArray. join ("* ¥"); // output result: What is it?
Document. write (myArray. join ("* &"); // output result: What is it?
Document. write (myArray. join (""); // output result: What is it?

Reverse (): reverse the order of elements in the array
Document. write (myArray. reverse (); // output: love, 3.1415, 1
Slice (<Start> [, <End>]): It is equivalent to array pruning. This does not include final. We can see that we should think of the substring () and substr () Methods of the Sting object .. In fact, they are all similar.
Var arraynumber = new Array (1, 2, 3, 4, 5, 6, 7, 8 );
Document. write (arraynumber. slice (3); // output result: 4, 5, 6, 7, 8
Document. write (arraynumber. slice (3, 5); // output result: 4, 5
I made a mistake. I wrote 4, 5, 6, and 5. Thank you for your suggestion. Note that the slice method does not include the termination location.
Document. write (arraynumber. slice (3, 3); // output result: What is it?
Document. write (arraynumber. slice (3, 2); // output result: What is it?
Document. write (arraynumber. slice (3,-1); // output result: What is it?
Document. write (arraynumber. slice (-100); // output result: What is it?

Sort ([<method function>]): sort
If there is no method function, sort by letter, that is, sort by character encoding, rather than numerical.
If there is a method function, it is sorted by method function.

Example:
Copy codeThe Code is as follows:
<Script>
Function sortNumber (a, B)
{
Return a-B;
}
Var myArray = new Array (3, 2, 54,23, 90,250 );
Document. write ("document. write (" numeric values not sorted by sort: ", myArray," <br/> ")
Document. write ("Default sort sorting value:", myArray. sort (), "<br/> ")
Document. write ("value sorted by sortNumber () sort:", myArray. sort (sortNumber), "<br/> ")
</Script>

The result is:
Values not sorted by sort: 3, 2, 54,23, 90,250
Default Value of sort sorting: I don't know. Who should remember character encoding.
Value of sort sorted by sortNumber (): 90,250
If you change "a-B" in the sortNumber method to "B-a", what is the result?

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.