Home > Web Front-end > JS Tutorial > Detailed explanation of function usage of js array

Detailed explanation of function usage of js array

王林
Release: 2020-03-31 09:14:36
forward
2320 people have browsed it

Detailed explanation of function usage of js array

This article explains the usage of several functions of JS arrays with examples. I hope it will be helpful to everyone.

The examples are as follows:

<html>
    <head>
        <title>js数组进阶</title>
        <meta charset="UTF-8"/>
        <script type="text/javascript">
            var arr=[2,"clannad","海贼王","星游记",true,new Date()];
//            console.log(arr);
            var a=arr.pop();   //删除数组的最后一个元素,返回值为最后一个元素。
//            console.log(a);
//            console.log(arr);
            var a2=arr.shift();  //删除数组的第一个元素,返回值为第一个元素。
//            console.log(a2);
//            console.log(arr);
            var a3=arr.splice(3,1,"月色真美");  //将数组中指定位置的数据替换为输入的数据,返回值为替换掉的元素。餐宿分别为在数组中的位置(起始位置),要替换的个数,替换的数值
//            console.log(a3);
//            console.log(arr);
            var arr2=["龙与虎","境界的彼方"];
            var m="轻音少女";
            var a4=arr.concat(arr2,m);    //数组的元素拼接。返回值为拼接后总数组。后面参数为要拼接在后面的数组。
//            console.log(a4);
//            console.log(arr);
            var a5=arr.join("-");      //数据的链接符,返回值为用输入的连接符链接后的数组元素,类型为string
//            console.log(a5);
//            console.log(arr);
            var a6=arr.push("言叶之庭");    //在数组的最后面添加一个数据,返回值为数组最终的长度。
//            console.log(a6);
//            console.log(arr);
            var a7=arr.unshift("冰菓");     //在数组的最开始添加一个数据,返回值为数据的最终长度。
//            console.log(a7);
//            console.log(arr);
            var a8=arr.reverse();       //将数组的元素倒序排列,返回值为倒序后的数组,原数组也被倒叙。
//            console.log(a8);
//            console.log(arr);
            console.log(arr.valueOf());       //一般的数组的valueof和tostring方法为打印数组的内容。
            console.log(arr.toString());
            var arrsort=[&#39;q&#39;,&#39;w&#39;,&#39;f&#39;,&#39;t&#39;,&#39;h&#39;,&#39;j&#39;];
//            console.log(arrsort);
            var arrsorted=arrsort.sort();
//            console.log(arrsorted);
//            console.log(arrsort);
        </script>
    </head>
    <body>
        <h3>js数组进阶</h3>
    </body>
</html>
Copy after login

Recommended related tutorials: js tutorial

The above is the detailed content of Detailed explanation of function usage of js array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template