This article mainly shares with you the detailed explanation of js array and sort method. The length of the array in js can be obtained and set. The original length is 6. When it is set to 3, the values in the array are only the first three. So it can be used to clear the array.
push(4), adds a 4 to the end of the array, pop() deletes one at the end, there are no parameters
shift(), deletes at the head, unshift(4) Add
splice (starting index value, deleted length) to the header, starting from 0, including before and after excluding
splice (starting index value, 0, characters to be inserted 'a', 'b', etc. ) The characters to be inserted are separated by commas
splice (starting point index value, length to be replaced, characters to be replaced)
a.concat(b) concatenates the two arrays a and b together
a.join('-'), connect the values of the array with -
a.sort(), sort the array, according to the first letter of the letters.
For numeric arrays, use the following method
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> //对于数值型数组,直接调用sort,它也会当做字符串型进行操作,即按照第一个字符1-9的顺序排列 //所以一使用下面的方法, var arr = [29,15,23,88,45]; arr.sort(function(n1,n2){ //简便写法如下 return n1-n2; /* if(n1>n2){ return -1; }else if(n1<n2){ return 1; }else{ return 0; } */ }); alert(arr); </script> </body></html>
Related recommendations:
js Detailed explanation of the differences between various sorting methods and sort methods
How the function sort() in javascript operates
The above is the detailed content of Detailed explanation of js array and sort method. For more information, please follow other related articles on the PHP Chinese website!