Conversion Method ToString (), tolocalstring (), ValueOf (), join ();
The valueof gets the array itself. The second and third get is an array, which actually invokes the ToString or toLocaleString function on each element.var a = [1,2,3,4,5]; Console.log (A.tolocalestring ()); // 1,2,3,4,5 Console.log (A.tostring ()); // 1,2,3,4,5 Console.log (A.valueof ()); //[1,2,3,4,5]
And it flattens the array:
var a = [1,2,3,4,5,[6,7,[8,9]]; Console.log (A.tolocalestring ()); // 1,2,3,4,5,6,7,8,9 Console.log (A.tostring ()); // 1,2,3,4,5,6,7,8,9
The Join method uses the given character to connect an array element to a string. The ToString method is actually called for each element.
Reorder methods Reverse (), sort ()
The reverse method is to reverse the array. var a = [1,2,3,4,5]; Console.log (A.reverse ()); // [5, 4, 3, 2, 1] Console.log (a); // [5, 4, 3, 2, 1]
As you can see, reverse is directly changing the array itself.
Another sort method accepts a function as the basis for sorting. If this argument is not passed, the ToString method is called on each element and then compared. That is, all converted to strings and then compared.
var a = [2,43,25,64,62,45,75,10,4,7] console.log (A.sort ()); // [2, 4, A, 7, ten] . Console.log (a); // [2, 4 , A, 7, ten].
If you pass in a function, the function has two parameters, which is two data to compare. such as function (A1,A2) {}; If you want the A1 to be in front, return less than 0; you want to return 0 in the same order; another situation you know. If you want to sort by the number of small to large.
var a = [2,43,25,64,62,45,75,10,4,7] console.log (a.sort (function(A, b) { return A- b; }); // [2, 4, 7, ten, +,--------] Console.log (a); // [2, 4, 7, ten, +,--------]
Operating methods Concat (), slice (), splice ()
Concat is to append an array to another array. var a = [1,2,3,4]; var b = [5,6,7,[7,8],9]; Console.log (A.concat (b))//[1, 2, 3, 4, 5, 6, 7, [7,8], 9] Console.log (a)//[ 1,2,3,4]; Console.log (b)//[5,6,7,[7,8],9]
you can see that concat does not flatten the array, and it does not change the original array . It wants to create a copy of a, then copy b in and then return.
Slice is based on the current array, creating a new array. Accepts two parameters, and the second parameter is optional. If there is only one parameter, it is the item from the specified position to the last one. If there are two parameters, it is the item from the first to the second specified position, but not the last one. The second argument can be a negative number, 1 represents the last item, but not the last one.
var a = [1,2,3,4,5]; Console.log (A.slice (1)); // [2,3,4,5] Console.log (A.slice (0, 3)); // [A] Console.log (A.slice (0,-3));; // [up]
Similarly, slice does not change the original array. And when the position of the second parameter precedes the position of the first parameter, an empty array is returned.
The splice feature is very powerful and can be removed, inserted, replaced
If only two parameters are passed, that is, delete, the first parameter represents the location to be deleted, and the second parameter represents the number of items to be deleted.
var a = [1,2,3,4,5]; Console.log (A.splice); // [2,3] Console.log (a); // [1,4,5]
If more than two parameters are passed in, and the second parameter is 0, it is inserted. The first parameter represents the position to be inserted, and the third and subsequent parameters represent the item to be inserted.
var a = [1,2,3,4,5]; Console.log (A.splice (1,0,7,8)); // [] Console.log (a); [1,7,8,2,3,4,5]
If more than two parameters are passed in, and the second parameter is not 0, it is a replacement. The first parameter represents the start position to be replaced, the second parameter represents how many items to replace, and the third and later parameters represent the items to be replaced.
var a = [1,2,3,4,5]; Console.log (A.splice (1,2,7,8)); // [2,3] Console.log (a); // [1,7,8,4,5]
Last look at an example:
var a = [1,2,3,4,5]; Console.log (A.splice (-1,2,7,8)); // [5] Console.log (a); // [1, 2, 3, 4, 7, 8]
This example shows that the first parameter accepts a negative number, and 1 represents the last item.