当前位置: 首页 > 科技观察

还在傻傻分不清ES5、Es6数组方法?各大姿势来袭

时间:2023-03-21 18:24:58 科技观察

还在为ES5和Es6的数组方法困惑吗?前言的本意:在面试中,面试官经常会问到Es5和Es6的数组方法。很多同学总是不清楚其中的区别。今天我将分享它。适合人群:前端初级开发Es5系列indexOf用途:用于查找数组中是否存在某个值,如果存在则返回某个值的下标,否则返回-1letlist=[1,2,3];console.log(list.indexOf(2))//1console.log(list.indexOf("frogman"))//-1map用途:map是一个数组函数方法,接收三个参数,value,index,self,返回值为处理后的结果。letlist=[1,2,3];constres=list.map((value,key,self)=>{console.log(value)//123console.log(key)//012console.log(self)//[1,2,3]returnvalue*2})console.log(res)forEach用途:用于遍历一个数组,接收三个参数,value,index,self,返回值为undefinedletlist=[1,2,3];constres=list.forEach((value,key,self)=>{console.log(value)//123console.log(key)//012console.log(self)//[1,2,3]return123})console.log(res)//undefinedsplice用途:用于删除或替换数组内容,接收三个参数:第一个参数为删除或添加的位置,第二个参数为要删除或添加的位数被删除,如果为0表示不删除第三个参数是向数组中添加内容letlist=[1,2,3];list.splice(0,1)//删除第0个位置console.log(list)//[2,3]list.splice(0,1,"frogman")//删除第0位,增加一个字符串console.log(list)//["frogman",2,3]list.splice(0,2,"Frogman")//从第0位开始删除2位,增加一个stringconsole.log(list)//["Frogman",3]slice用途:用于截取数组值,接受两个参数。第一个参数是要获取的值的下标,第二个参数是要截取的下标的前一位。letlist=[1,2,3];letres=list.slice(1,3)//从第一个下标截取到第三个下标的前一个,所以截取的是[2,3]console.log(res)//[2,3]filter用途:用于过滤数组中符合条件的值,返回值为满足条件的数组对象letlist=[1,2,3];letres=list.filter(item=>item>1);console.log(res)//[2,3]every用途:用于检测数组的所有元素是否满足指定条件,返回值为Boolean,该方法需要数组中的所有值如果元素满足条件则返回true,否则返回false)console.log(res1)//falsome目的:用于检测数组中的元素是否满足指定条件,返回值是布尔值。该方法只要数组中有满足条件的item就返回true,否则falseletlist=[1,2,3];letres=list.some(item=>item>0)console.log(res)//truereduce目的:该方法接收一个作为累加器的函数,数组中的每个值(从左到右)开始Reduction,最终计算出一个值。该方法的回调函数接收四个参数。第一个参数:初始值,或者计算后的返回值。第二个参数:当前元素。第二个参数:当前元素的索引。第四个参数:当前元素所属的数组对象,我们一般只用前两个。reduce第一个参数为回调函数,第二个参数为初始值letlist=[1,2,3];letres=list.reduce((prev,cur)=>prev+=cur,0)console.log(res)//6reverse目的:用于数组反转letlist=[1,2,3];letres=list.reverse();console.log(res)//[3,2,1]join目的:是什么形式用于拼接数据letlist=[1,2,3];letres=list.join("-");console.log(res)//1-2-3letsum=eval(list.join("+"))console.log(sum)//6sort用途:用于对数组进行排序。排序规则的返回值为正数,后面的数在前面为负数。数字不变,之前的返回值为0,所以letlist=[1,2,3];letsort=list.sort((a,b)=>b-a)console.log(sort)//[3,2,1]concat用途:用于合并数组originalletlist=[1,2,3];letres=list.concat([1,2,3])console.log(res)//[1,2,3,1,2,3]push目的:向数组后面添加元素,返回值为数组的长度letlist=[1,2,3];letres=list.push(1)console.log(res)//4pop用途:用于删除数组末尾的元素,返回值为被删除的元素letlist=[1,2,3];letres=list.pop()console.log(res)//3shift用途:用于删除数组头部,返回值为被删除的元素letlist=[1,2,3];letres=list.shift()console.log(res)//1unshift用途:添加元素到数组头部,返回值为数组的lengthletlist=[1,2,3];letres=list.unshift(1)console.log(res)//4toString用途:用于将数组内容转换成字符串letlist=[1,2,3];letres=list.toString()console.log(res)//1,2,3Es6+includes目的:检查元素是否存在于数组中,返回布尔值letlist=[1,2,3];letres=list.includes("Frogman")letres1=list.includes(1)console.log(res,res1)//falsetruefind目的:查找数组的元素,满足条件返回单个值,返回letlist=[1,2,3]根据就近原则;letres=list.find((item)=>item>1)console.log(res)//2,根据就近原则返回findIndex目的:查找元素中的元素数组,并返回满足条件的数组下标letlist=[1,2,3];letres=list.findIndex((item)=>item>1)console.log(res)//1,根据返回下标proximityflat原则目的:用于对嵌套数组对象进行扁平化letlist=[1,2,3,[4,[5]]];letres=list.flat(Infinity)console.log(res)//[1,2,3,4,5]fill用途:用来填充数组对象letlist=[1,2,3];letres=list.fill(1)console.log(res)//[1,1,1]Array.isArray目的:检查对象是否为数组letlist=[1,2,3];letres=Array.isArray(list)console.log(res)//trueArray。fromPurpose:Convertpseudo-arraytotruearrayletres=Array.from(document.getElementsByTagName("div"))console.log(res)//转换为真实数组调用数组原型方法Array.ofPurpose:Used生成一个数组对象,主要用来弥补Array()Insufficientletres=Array.of(1,2,3)console.log(res)//[1,2,3]改变原数组的值splice,reverse,sort,push,pop,shift,unshift,fillConclusion这里不写键、值和条目。如果他们使用数组方法,则返回Iterator遍历器对象。欢迎使用查漏补缺常用数组法。