美文网首页
数组方法集合

数组方法集合

作者: 阿拉斯加南海岸线 | 来源:发表于2022-09-14 16:05 被阅读0次

    1、检测数组

    Array.isArray(value)
    

    2、转换方法

    // 将数组转换成字符串,以 | 号分隔
    array.join(' | ') 
    

    3、栈方法

    栈是一种 LIFO(Last-In-First-Out,后进先出)的数据结构,也就是最新添加的项最早被移除。
    栈中项的插入(叫做推入,push())和移除(叫做弹出,pop()),只发生在一个位置——栈的顶部

    var count = colors.push("red", "green"); // 推入两项
    alert(count); //2 
    count = colors.push("black"); // 推入另一项
    alert(count); //3 
    var item = colors.pop(); // 取得最后一项
    alert(item); //"black" 
    alert(colors.length); //2
    

    4、队列方法

    队列数据结构的访问规则是 FIFO(First-In-First-Out,先进先出)。队列在列表的末端添加项,从列表的前端移除项。push()是向数组末端添加项的方法,从数组前端取得项的方法。实现这一操作的数组方法就是 shift(),它能够移除数组中的第一个项并返回该项,同时将数组长度减 1。
    结合使用 shift()和 push()方法,可以像使用队列一样使用数组。

    var colors = new Array(); //创建一个数组
    var count = colors.push("red", "green"); //推入两项
    alert(count); //2 
    count = colors.push("black"); //推入另一项
    alert(count); //3 
    var item = colors.shift(); //取得第一项
    alert(item); //"red" 
    alert(colors.length); //2
    

    ECMAScript 还为数组提供了一个 unshift()方法。顾名思义,unshift()与 shift()的用途相反:它能在数组前端添加任意个项并返回新数组的长度。因此,同时使用 unshift()和 pop()方法,可以从相反的方向来模拟队列,即在数组的前端添加项,从数组末端移除项。

    5、操作数组项方法

    5.1 concat()

    方法可以基于当前数组中的所有项创建一个新数组。
    —— 未传参,复制当前数组;
    ——传参 N,合并为一个新数组

    var colors = ["red", "green", "blue"]; 
    var colors2 = colors.concat("yellow", ["black", "brown"]); 
    alert(colors); //red,green,blue 
    alert(colors2); //red,green,blue,yellow,black,brown
    
    5.2 slice(),能够基于当前数组中的一或多个项创建一个新数组。

    ——参数1,数组开始的位置,必选;
    ——参数2,数组结束为止,非必选,没有到数组末尾;

    var colors = ["red", "green", "blue", "yellow", "purple"]; 
    var colors2 = colors.slice(1); 
    var colors3 = colors.slice(1,4); 
    alert(colors2); //green,blue,yellow,purple 
    alert(colors3); //green,blue,yellow
    
    5.3 splice()方法

    ——删除:可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数。例如,splice(0,2)会删除数组中的前两项。
    ——插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、0(要删除的项数)和要插入的项。如果要插入多个项,可以再传入第四、第五,以至任意多个项。例如,splice(2,0,"red","green")会从当前数组的位置 2 开始插入字符串"red"和"green"。
    ——替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。例如,splice (2,1,"red","green")会删除当前数组位置 2 的项,然后再从位置 2 开始插入字符串"red"和"green"。

    var removed = colors.splice(0,1); // 删除第一项
    alert(colors); // green,blue 
    alert(removed); // red,返回的数组中只包含一项
    removed = colors.splice(1, 0, "yellow", "orange"); // 从位置 1 开始插入两项
    alert(colors); // green,yellow,orange,blue 
    alert(removed); // 返回的是一个空数组
    removed = colors.splice(1, 1, "red", "purple"); // 插入两项,删除一项
    alert(colors); // green,red,purple,orange,blue 
    alert(removed); // yellow,返回的数组中只包含一项
    

    6、位置方法

    indexOf()和 lastIndexOf(),返回在数组中的位置。

    7、迭代方法

    ——every():对数组中的每一项运行给定函数,如果该函数对每一项都返回 true,则返回 true。
    —— filter():对数组中的每一项运行给定函数,返回该函数会返回 true 的项组成的数组。
    —— forEach():对数组中的每一项运行给定函数。这个方法没有返回值。
    —— map():对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。
    —— some():对数组中的每一项运行给定函数,如果该函数对任一项返回 true,则返回 true。
    以上方法都不会修改数组中的包含的值。

    var numbers = [1,2,3,4,5,4,3,2,1]; 
    var everyResult = numbers.every(function(item, index, array){ 
     return (item > 2); 
    }); 
    alert(everyResult); //false 
    
    var someResult = numbers.some(function(item, index, array){ 
     return (item > 2); 
    }); 
    alert(someResult); //true
    
    var numbers = [1,2,3,4,5,4,3,2,1]; 
    var filterResult = numbers.filter(function(item, index, array){ 
     return (item > 2); 
    }); 
    alert(filterResult); //[3,4,5,4,3]
    
    var numbers = [1,2,3,4,5,4,3,2,1]; 
    var mapResult = numbers.map(function(item, index, array){ 
     return item * 2; 
    }); 
    alert(mapResult); //[2,4,6,8,10,8,6,4,2]
    
    var numbers = [1,2,3,4,5,4,3,2,1]; 
    numbers.forEach(function(item, index, array){ 
     //执行某些操作
    });
    

    8、归并方法

    reduce()和 reduceRight()。这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。

    var values = [1,2,3,4,5]; 
    var sum = values.reduce(function(prev, cur, index, array){ 
     return prev + cur; 
    }); 
    alert(sum); //15
    

    第一次执行回调函数,prev 是 1,cur 是 2。第二次,prev 是 3(1 加 2 的结果),cur 是 3(数组的第三项)。这个过程会持续到把数组中的每一项都访问一遍,最后返回结果。
    reduceRight()的作用类似,只不过方向相反而已。

    相关文章

      网友评论

          本文标题:数组方法集合

          本文链接:https://www.haomeiwen.com/subject/xhgkortx.html