The last time I said the elements of the array of additions and deletions, this time to say some of the array operation methods
Join () method :
1 var arr=[1,2,3]2 arr.join ("_" ) //1_2_3
The Join method returns a string that is concatenated as a string of each value in the array with the Join method argument as a connector, and the join method parameter is concatenated with commas if it is null, similar to the value obtained by the ToString () method.
reverse () method:
1 var arr=[2,1,32//arr=[3,1,2]
The reverse () method changes the array so that the order of the elements in the array is reversed, and the sort () method has a small trick that can be used to achieve this effect.
The Tip:reverse () method simply reverses the position order, not by numerical size or by encoding rules.
sort () method :
1 vararr1=["Lebro","Wade","Duncan"]; 2Arr1.sort ();//["Duncan", "Lebro", "Wade"]3 vararr2=[ -, the,3, $, -];4Arr2.sort ();//[3 , +]
By default, the sort () method arranges array items in ascending order, first calling the ToString () method of each array item, and then comparing the resulting string to determine how to sort, so there is a number in the upper part of the code that is not sorted according to the size of the numeric value that we want. W3cschool gives such an explanation:
If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded 0 The value of 00 .
To tell the truth, the description of the comparison function is not difficult to understand, but the operation of the comparison function I still more difficult to understand, the bottom gives me to learn this comparison function of a piece of code and summary (because I do not understand the study, so wrote out, crossing to think that they do not like can skip)
First of all, it is this so-called comparison function that prints out each parameter and looks at the real-time changes in arr:
1 vararr=[ in,5, One,2, +];2 Arr.sort (sortattr);3 varSortattr=function (A, b) {4 Console.log (arr);5Console.log ("a="+a);6Console.log ("b="+b);7Console.log ("a-b="+ (A-B));8 returnA-B;9};
Here's what the Chrome browser prints:
You can see this so-called comparison function, he will compare the array element 221, return a value, if the previous value is less than the next value, then return a negative number, the order does not change. If the previous value is greater than the last value, then the two order is exchanged, and then compared with the ordered elements in the front, until the first one is the same, it will always be the smallest, and so on until the final element is ordered to end the entire loop in this way, the 22 comparison is somewhat similar to the one behind the reduce () function, again.
As for the implementation of a small method like the reverse () method, only the whole number of the array inversion, you can deepen the understanding, only need to change the return value of the comparison function to a fixed return to a negative number, so that the elements behind will continue to the front of the order, complete the reversal. The sort () method will also change the original array.
Concat () method
JavaScript Arrays about 2