es5/6数组遍历以及常用的一些方法
数组的遍历方法 1...for(var i=0;i<arr.length;i++){ } ------------------------------------------------------------ 2...arr.forEach(function(val,index,arr){ console.log(val,index,arr) //第三个参数就是原数组可要可不要; }) arr.forEach((val,index,arr)=>{ console.log(this,val,index,arr) //第三个参数就是原数组可要可不要; },bind(123)) //this指向会变箭头函数; ------------------------------------------------------------ 3...arr.map((val,index,arr)=>{ console.log(val,index,arr) //正常需要返回值,并且返回的新的一个数组; }) 注意:一般需要有return 没有则相当于forEach;重新整理数据结构 ------------------------------------------------------------ 4...arr.filter((val,index,arr)=>{ return val.xxx==条件; //过滤不合格的元素; }) ----------------------------------------------------------- 5...arr.some((val,index,arr)=>{ //类似查找,数组某个元素符合则返回true }) let arr = [1,2,3] let newArry = arr.some((val,index,arr)=>{ return val==2 }) console.log(newArry) //true; ----------------------------------------------------------- 6...arr.every((val,index,arr)=>{ //数组每个元素符合则返回true }) let arr = [1,3,5] let newArry = arr.every((val,index,arr)=>{ return val%2==1 }) console.log(newArry) //true; ----------------------------------------------------------- 7...arr.reduce((prev,now,index,arr)=>{ return prev+now; //求数组的和、阶乘;第一个参数表示前一个,第二个表示当前 }) let arr = [1,2,3] let sum = arr.reduce((prev,now,index,arr)=>{ return prev+now; //阶乘Math.pow(prev,now)输出 }) console.log(sum) //输出6; ------------------------------------------------ let arr = [2,2,3] let sum = arr.reduce((prev,now,index,arr)=>{ return Math.pow(prev,now); //阶乘prev**now }) console.log(sum) //输出64; ----------------------------------------------------------- 8...for(let val of arr){ //arr.keys() //数组下标 //arr.entries() //数组的某一项 } let arr1=[1,2,3,4]; for(let a of arr1.keys()){ console.log(a) //结果:0,1,2,3遍历了数组arr的索引 } -------------------------------------------------- let arr2=[1,2,3,4]; for(let a of arr2.values()){ console.log(a) //结果:1,2,3,4遍历了数组arr的值 } -----------------------------------------------------------
扩展运算符: ... let arr = [1,2,3] let arr2 = [...arr] //es6 let arr3 = Array.form(arr) //es6 let arr4 = [].call(arr) //es5 -------------------------------------------------------------- Array.form:把类数组(获取一组元素、arguments)对象转成数组;如ul li;具备length属性的; 特别的json: let json = { 0:"a", 1:"b", 2:"c", length:3 } let newArr2 = Array.form(json) console.log(newArr2) //["a","b","c"] 特别的String: let str = "stfssfg"; let arr1 = str.split("");//es5 console.log(arr1) let arr2 = Array.form(str) console.log(arr2) -------------------------------------------------------------- Array.of();把一组值转成数组; let arr = Array.of("xx1","xx2","xx3") console.log(arr) //["xx1","xx2","xx3"] -------------------------------------------------------------- arr.find() 查找找出第一个符合条件的数组成员;没找到返回undefined; let arr = [1,10,20,200,90]; let newArr = arr.find((val,index,arr)=>{ return val>100; }) console.log(arr) //200; -------------------------------------------------------------- arr.findindex() 查找位置;没找到返回-1; let arr = [1,10,20,200,90]; let newArr = arr.findindex((val,index,arr)=>{ return val>1000; }) console.log(arr) //-1; -------------------------------------------------------------- arr.fill(填充的东西,开始位置,结束位置) let arr = new Arry(10); arr.fill("默认值",1,5) -------------------------------------------------------------- arr.includes() 判断数组里面是否包含这个值; let arr = [1,2,3,4]; let arr1 = arr.includes(2); cconsole.log(arr1) //true;布尔值