ES6中新增关于Array的方法
在javascript中,Array()经常用到,利用ES6中的一些新特性会让数组的处理更加方便快捷 1.迭代空数组 直接创建一个数组 const arr=new Array(4); //Output:[undefined,undefined,undefined,undefined] 利用map方法,转化成新的数组,企图得到 [0,1,2,3] 数组 const arr=new Array(4); arr.map((ele,index) => index); //Output:[undefined,undefined,undefined,undefined] 解决这个问题可以在创建数组时用到Array.apply apply与call类似,都是用来继承父类的方法的,不同之处是: call() 方法分别接受参数。person.fullName.apply(person1, ["Oslo", "Norway"]); apply() 方法接受数组形式的参数. person.fullName.call(person1, "Oslo", "Norway"); 如果要使用数组而不是参数列表,则 apply() 方法非常方便。 const arr = Array.apply(null, new Array(4)); arr.map((ele,index) => index); //Output:[0,1,2,3] 由此,我们可以创建一个指定最大值、最小值、或者长度生成指定数列的方法 /** * 生成自定义的连续数列 * @param{Number}min * @param{Number}max * @param{Number}len */ function newArr({min = null, max = null, len = null} = {}) { let newArray=[], skip = min if (len == null) {len = max - min + 1} if (min == null) {skip = -max} const arr = Array....