zoukankan      html  css  js  c++  java
  • 操作数组的方法

    公司要做的三合一新项目,看了下,需要了解HTML5、CSS3、jQuery、bootsrap等。本来我想直接看jQuery的,但是据说JavaScript是基础啊,哎,谁让它是基础呢,只能拿本书先看看基础的基础了。

    想到要写博客前,我其实已经看了几章了。当时没想到记录,算了算了,前面看的注意点以后再回顾到再记录,毕竟还有jQuery和bootsrap在等着我。

    正好看到数组这一章节,操作数组的各种方法,感觉有必要写下来,以防忘记。

    • 栈方法:
    • push():接受任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度。
    • pop():从数组末尾移除最后一项,减少数组length长度,然后返回移除项。

    这里要注意的是新增的位置以及最后返回的值。看下面代码:

    <script>
            var color=new Array();
        var count=color.push("blue","red","yellow","green");
        alert(count);//返回4
        
            var item=color.pop();
        alert(item);//返回green
    </script>         
    • 队列方法:
    • push():和栈方法中的push是同一个。
    • shift():移除数组中第一个项,并返回该项。与pop比,就是移除项位置的区别。
    • unshift():在数组前端添加任意个项,并返回新数组长度。和push比就是新增项位置的区别。
    • 重排序方法:
    • reverse():反转数组项的位置。
    • sort():默认情况下,sort按从小到大排序。sort会先调用toString()方法,然后比较所得字符串,如果是数值也会先转化为字符;sort方法也可以用一个比较函数作为参赛,以便指定哪个位置在前,哪个在后。
    <script>
            var color=[0,1,5,10,15];
            
                
            function compare(value1,value2){
                if (value1<value2){
                    return -1;
                }
                    
                else{
                    return 1;
                }
            }
                
                
            color.reverse();
            alert(color);  //15,10,5,0,1
                
                
                
            color.sort();
            alert(color);/*0,1,10,15,5,这里没出错,因为比较的是字符大小,所以5在最后*/
                
            color.sort(compare);//注意此次compare后面没有()
            alert(color);    //0,1,5,10,15
            </script>
    • 操作方法:
    • concat():基于当前数组创建一个新数组,不会影响原数组。
    • slice():基于当前数组的一个或多个创建新数组,可接受一个或两个参数。如果是一个参数则返回该参数指定的位置,到原数组最后;如果两个参数,则返回起始和结束位置之间的项,但不包括结束位置。注意,数组下标均是从0开始。该方法也不影响原数组。
    • splice():主要是用于向数组的中部插入项。使用方式有三种:①删除:指定2个参数——删除第一项的位置和删除的项数,splice(1,2);②插入:指定三个参数——删除第一项的位置、删除的项数、插入的项,splice(2,0,"yellow","green");③替换:可以指定位置插入任意数量的项,同时删除任意数量的项,splice(2,1,"red","green")。其实我觉得这三种就一个意思:可以指定位置插入任意数量的项,同时删除任意数量的项。注意该方法影响的是原数组,返回的是删除的项,如果没删除的项,则返回空数组。
    <script>
                var color=[0,1,5,10,15];
                
                var color2=color.concat(2,3,[4,5],6);
                alert(color2); //0,1,5,10,15,2,3,4,5,6
                
                var color3=color.slice(1);
                var color4=color.slice(1,3);
                alert(color3); //1,5,10,15
                alert(color4);//1,5
                
                var remove=color.splice(0,1);
                alert(remove);//0
                alert(color);//1,5,10,15
                
                remove=color.splice(1,0,2,3);
                alert(remove);//空数组
                alert(color);//1,2,3,5,10,15
                
                remove=color.splice(1,1,2,4);
                alert(remove);//2
                alert(color);//1,2,4,3,5,10,15
                
            </script>
    • 位置方法:
    • indexOf():从开始位置开始找,所带参数——查找项和可选的起始项。
    • lastIndexOf():从结束位置开始找,所带参数——查找项和可选的起始项。

    在没找到的情况下,返回-1,查找过程的比较是用全等"==="进行比较。

    <script>
                var color=[0,1,5,10,5,5,15];
                
                alert(color.indexOf(2,5));//-1
                alert(color.indexOf(5,2));//2
                alert(color.indexOf(5,3));//4
                alert(color.lastIndexOf(5));//5
                alert(color.lastIndexOf(5,4));//4
                
    </script>
    • 迭代方法:
    • every():对数组每一项运行给定函数,如果均true,则返回true。
    • some():对数组每一项运行给定函数,若其中任一项函数返回结果为true,则为true。
    • filter():对数组每一项运行给定函数,返回会返回true的项组成的数组。
    • map():对数组每一项运行给定函数,返回每次函数调用结果组成的数组。
    • forEach():对数组每一项运行给定函数,无返回值。
    <script>
                var color=[0,1,2,3,4,5,6,];
                var everyResult=color.every(function(item,index,arry){
                    return (item>2);
                });
                alert(everyResult);//false
                
                var someResult=color.some(function(item){
                    return(item>2);
                });
                alert(someResult);//true
    
                var filterResult=color.filter(function(item){
                    return (item>2);
                });
                alert(filterResult);//3,4,5,6
                
                var mapResult=color.map(function(item){
                    return item*2;
                });
                alert(mapResult);//0,2,4,6,8,10,12
                
                var forEachResult=color.forEach(function(item){
                    //执行某些操作
                })
            </script>
    •  归并方法:
    • reduce()、reduceRight():迭代所有的项,然后构建一个最终的返回值。区别只是一个是从数组头部开始,一个从尾部开始。
    <script>
                var color=[0,1,2,3,4,5,6,];
                var sum1=color.reduce(function(prev,cur,index,array){
                    return prev+cur;
                })
                alert(sum1);//所有项相加,21
            </script>
  • 相关阅读:
    解决RobotFramework的关键字不能高亮的问题
    使用Python遇到:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 问题
    通过Jekins执行bat脚本始终无法完成
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
    [转]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    HDU 2686 MCMF
    HDU 4278 卡特兰,区间DP
    POJ 2985 名次树
    POJ 2531 深搜剪枝
    Uva 10061 进制问题
  • 原文地址:https://www.cnblogs.com/xwtcm/p/7266882.html
Copyright © 2011-2022 走看看