First, the definition of the array and the initialization method:
var myArray = new Array(1,3.1415,"love"); //Note here that the elements in the myArray array are not just elements of the same data type, they can be integers, floating point types, strings, etc. This fully demonstrates the weakening of data types by JavaScript as a language, making the language more casual and simplified. Just use var when defining an object.
The introduction here is limited, and there are some that I have not given the results. I hope you can experience it yourself and try it yourself to see what the results are. This will help with memory. The following parameters with [] can be omitted.
Properties of Array:
length: The length of the array object, that is, the number of array elements. It should also be noted here that the subscript of the first element is 0.
document.write(myArray.length); //The result is 3
Array methods:
Copy the code code as follows:
join(<separator>): Connect the elements in the array one by one, using the separator between the 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(): Reverse the order of elements in the array
document.write(myArray.reverse()); //Output result: love,3.1415,1
slice(<start>[,<end>]): Equivalent to cutting the array, the end is not included here. When you see this, you 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 results: 4,5,6,7,8
document.write(arraynumber.slice(3,5)); // Output result: 4,5
I made a mistake. The result I originally wrote was 4,5,6, but it was actually 4,5. Thanks to a friend for bringing this up. Please note that 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 function, sort in alphabetical order, that is, sort according to the order of character encoding, not sorting by numerical value as is usually considered.
If it has a method function, it will be sorted by the method function.
example:
Copy the code code as follows:
<script>
function sortNumber(a,b)
{
return ab;
}
var myArray = new Array(3,2,54,23,90,250);
document.write("document.write("Unsorted values: ",myArray,"<br />")
document.write("Default sort sorting value:",myArray.sort(),"<br />")
document.write("Numbers sorted by sortNumber(): ",myArray.sort(sortNumber),"<br />")
</script>
turn out:
Unsorted values: 3,2,54,23,90,250
Default sort value: I don’t know this either. Who should remember the character encoding?
Values sorted by sortNumber(): 2,3,23,54,90,250
If you change "ab" in the sortNumber method to "ba", what will be the result?