Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js array and sort method

小云云
Release: 2018-03-14 16:45:57
Original
1247 people have browsed it

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>
Copy after login

Related recommendations:

How to use sort() in js

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template