Array object in JavaScript for details

Source: Internet
Author: User
Tags join numeric value

  This article mainly introduces the array object in JavaScript, which can be referenced by friends in the following

First, the definition of the array and the initialization method:  var myarray = new Array (1,3.1415, "Love"); Notice here that the elements in the myarray array are not only elements of the same data type, they can be shaped, have floating-point types, have strings, and so on. This shows that JavaScript as a language for the weakening of data types, language more casual, simple. Use Var when defining an object on a row.   In this introduction of the limited, at the same time there are some I did not give results, I hope that everyone to experience themselves, try to see what the results are, so as to help the memory. The following bands [] are parameters that can be omitted.     Array Property:  Length: The length of the array object, that is, the number of elements in the array. Here also note that the subscript of the first element is 0.  document.write (myarray.length); The result is the 3    array method:  code as follows: Join (< separator >): Concatenate each element in the array, and place the delimiter between elements and elements   document.write ( Myarray.join ("-")); Output results: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 (): Reverses the order of elements in an array   document.write (Myarray.reverse ()); Output Results:love,3.1415,1  Slice (< >[,< final): the equivalent of array clipping, not including the end. See here you should associate the substring () and substr () method of Sting objects. are actually similar.   var arraynumber = new Array (1,2,3,4,5,6,7,8); &nBsp document.write (Arraynumber.slice (3)); The output results:4,5,6,7,8  document.write (Arraynumber.slice (3,5)); Output:4,5  I made a mistake, I originally wrote the result is 4,5,6, is actually 4, 5. Thanks for a friend's offer. Attention, in fact, the slice method does not include the termination position.   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   Without method functions, sorted alphabetically, that is, sorted by character encoding, not generally considered numerically sorted.   If you have a method function, sort by the method function.     Example:    code as follows: <script>  function Sortnumber (a,b)   {  return a-b; }   var myarray = new Array (3,2,54,23,90,250);  document.write ("document.write (" not sorted by sort: ", myarray," < br/> ")   document.write (" Default sort Sort value: ", Myarray.sort ()," <br/> ")   document.write (" via Sortnumber "() Sort sorted values: ", Myarray.sort (Sortnumber)," <br/> ")   </script>    result is:  without sortThe numeric value of the sort:3,2,54,23,90,250  the default sort order: This I don't know, the main who will remember the character encoding.   Value of sort sorted by Sortnumber ():2,3,23,54,90,250  If you change the "A-b" in the Sortnumber method to "B-a", what is the result?    
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.