Array Array objects in Javascript

  • 2020-03-30 02:10:02
  • OfStack

First of all, the definition of array and the initialization method:
Var myArray = new Array(1,3.1415,"love"); // note here that the elements in an array of myArray are not just elements of the same data type, they can be plastic, floating point, string, etc. This fully shows the weakening of javascript as a language to data types, and the language is more arbitrary and simple. Just like we use var when we define objects.
I'm going to give you a little bit of an introduction here, but some of the results I didn't give you, so I want you to try it yourself, to try it yourself, to see what the results are, to help you remember. The following parameters with [] can be omitted.

Properties of Array:
Length: the length of an array object, the number of elements in the array. Again, notice that the index of the first element is 0.
Document. The write (myarray.length); // that's 3

Array method:
join(< The separator >): Join the elements of the array one by one, placing the separator between the elements  
document.write(myArray.join("-")); //Output result: 1-3.1415-love
document.write(myArray.join(" ")); //Output: what is it?
document.write(myArray.join("* RMB ")); //Output: what is it?
document.write(myArray.join("* &")); //Output: what is it?
document.write(myArray.join(" ")); //Output: what is it?

reverse(): Reverse the order of the elements in the array  
document.write(myArray.reverse()); //Output result: love,3.1415,1
slice(< beginning >[,< The final >]): It's equivalent to clipping an array , The end is not included here. You should think about it here Sting The object's substring() and substr() There you go. It's all the same.  
var arraynumber = new Array(1,2,3,4,5,6,7,8); 
document.write(arraynumber.slice(3)); //Output: 4,5,6,7,8
document.write(arraynumber.slice(3,5)); //Output: 4,5
i made a mistake That's what I wrote before 4,5,6 , it is 4,5 . Thanks to a friend for bringing it up. I want you to pay attention , Actually, slice Method does not include the termination position.  
document.write(arraynumber.slice(3,3)); // Output: what is it?
document.write(arraynumber.slice(3,2)); // Output: what is it?
document.write(arraynumber.slice(3,-1)); // Output: what is it?
document.write(arraynumber.slice(-100)); //Output: what is it?

Sort ([< method function >]] ) : sorting
Sort alphabetically without method functions, that is, sort by character code, not by value as is often thought.
If you have a method function, sort by method function.

function sortNumber(a,b) 
return a-b; 
var myArray = new Array(3,2,54,23,90,250); 
document.write("document.write(" without sort Sorting value: ",myArray,"<br />") 
document.write(" The default sort Sorting value: ",myArray.sort(),"<br />") 
document.write(" the sortNumber() the sort Sorting value: ",myArray.sort(sortNumber),"<br />") 

The result is:
Unsorted values: 3,2,54,23,90,250
Default sort sort value: I don't know about that either, but who mostly remembers the character encoding.
The value of sort sort by sortNumber() : 2,3,23,54,90,250
If you change "a-b" in sortNumber to "b-a", what do you get?

Related articles: