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 their own experience, to try their own hands to see what the results are, so as to help the memory. The following bands [] are parameters that can be omitted.
Array's properties:
Length: The size of the array object, that is, the number of elements in the array. Also notice here that the subscript for the first element is 0.
document.write (myarray.length); The result is 3.
Array method:
Copy Code code as follows:
Join (< separator >): Connect the elements of an array one by one, and place delimiters between elements
document.write (Myarray.join ("-")); Output Result: 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 Result: love,3.1415,1
Slice (< >[,< end): the equivalent of array clipping, this does not include 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);
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 originally wrote the result is 4,5,6, actually is 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): Sorting
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:
Copy Code 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" ("value not sorted by sort:", myarray, "<br/>")
document.write ("Default sort Sort value:", Myarray.sort (), "<br/>")
document.write ("Sort by Sortnumber ():", Myarray.sort (Sortnumber), "<br/>")
</script>
The result:
Values not sorted by sort: 3,2,54,23,90,250
Numeric value of the default sort sort: I don't know, who's going to memorize the character code?
Value sorted by Sortnumber (): 2,3,23,54,90,250
If we change the "A-b" in the Sortnumber method to "B-a", what is the result?