当前位置: 首页 > Web前端 > JavaScript

JavaScript数组常用方法总结

时间:2023-03-27 16:52:40 JavaScript

这是JavaScript数组中一些操作方法的总结。希望对大家有所帮助。1.unshift()方法向数组头部添加任意数量的元素,并返回数组的新长度。会改变原来的数组.letarr=[2,3,4,5];console.log(arr.unshift(3,6));//6数组长度console.log(arr);//打印结果[3,6,2,3,4,5]2.shift()方法删除数组头部的一个元素,并返回删除的元素,改变原来的数组。letarr=[2,3,4,5];让Array=arr.shift()console.log(Array);//2返回值console.log(arr);//打印结果[3,4,5]3.push()方法添加任意元素,并返回新的数组长度,将改变原数组vara=[2,3,4];varb=a.推送(5);console.log(a);//[2,3,4,5]console.log(b);//44.pop()方法删除数组末尾的一个元素。返回删除的元素将更改原始数组。vararr=[2,3,4];console.log(arr.pop());//4console.log(arr);//[2,3]5.concat()方法用于合并两个或多个数组。此方法不会改变现有数组,而是返回一个新数组。varalpha=['a','b','c'];变量数字=[1,2,3];alpha.concat(numeric);//resultin['a','b','c',1,2,3]6.join()方法连接数组(或类数组对象)的所有元素成一个字符串并返回这个字符串。元素之间用指定的分隔符分隔,默认使用','进行分割,不改变原数组。constelements=['火','空气','水'];console.log(elements.join());//预期输出:"Fire,Air,Water"console.log(elements.join(''));//预期输出:"FireAirWater"console.log(elements.join('-'));//预期输出:"Fire-Air-Water"7.splice()方法可以在任意位置删除或在任意位置添加元素,并返回数组中修改后的内容。此方法会改变原始数组。参数含义:(1.从哪里开始删除,2.删除的元素个数,3.添加的元素)vara=[5,6,7,8];console.log(a.splice(1,0,9));//[]console.log(a);//[5,9,6,7,8]varb=[5,6,7,8];console.log(b.splice(1,2,3));//[6,7]console.log(b);//[5,3,8]8.slice()方法返回一个新的数组对象,这个对象是由begin和end(包括begin,不包括end)决定的原数组的浅拷贝。原数组不会改变。vararr=[2,3,4,5];console.log(arr.slice(1,3));//[3,4]console.log(arr);//[2,3,4,5]10.sort(),按照Unicode码位置排序,默认升序varfruit=['樱桃','苹果','香蕉'];fruit.sort();//['苹果','香蕉','樱桃']varscores=[1,10,21,2];分数.排序();//[1,10,2,21]11.reverse()方法用于反转数组中元素的顺序。返回反转数组,改变原始数组。vararr=[1,2,3,4];console.log(arr.reverse());//[4,3,2,1]console.log(arr);//[4,3,2,1]12.indexOf()和lastIndexOf()都接受两个参数:搜索到的值和搜索到的起始位置不存在,返回-1;存在,返回位置。indexOf是从前到后查找;lastIndexOf是从后往前查找。indexOf用法vara=[2,9,9];a.indexOf(2);//0a.indexOf(7);//-1if(a.indexOf(7)===-1){//如果在数组中找不到7,完成}lastIndexOf用法varnumbers=[2,5,9,2];numbers.lastIndexOf(2);//3numbers.lastIndexOf(7);//-1numbers.lastIndexOf(2,3);//3numbers.lastIndexOf(2,2);//0numbers.lastIndexOf(2,-2);//0numbers.lastIndexOf(2,-1);//313.forEach()数组遍历constarray1=['a','b','c'];array1.forEach(element=>console.log(element));//期望输出:"a"//预期输出:“b”//预期输出:“c”14.filter(),对数组的每一项运行给定的函数,返回一个数组constwords=['spray','limit','elite','exuberant','destruction','present'];constresult=words.filter(word=>word.length>6);console.log(result);//预期输出:Array["exuberant","destruction","present"]15.map(),对数组的每一项运行给定的函数,返回每个函数调用的结果,组成一个新的数组varnumbers=[1,5,10,15];vardoubles=数字。map(function(x){/*arr.map(functioncallback(currentValue[,index[,array]])参数:1.currentValue|回调数组中正在处理的当前元素2.index可选|回调数组中正在处理的当前元素的索引3.array可选|map方法调用的数组/*returnx*2;});//双打现在是[2,10,20,30]//数字仍然是[1,5,10,15]16.includes()方法是用于判断一个数组是否包含指定值,根据情况,如果包含则返回true,否则返回false。常量数组1=[1,2,3];console.log(array1.includes(2));//预期输出:trueconstpets=['cat','dog','bat'];console.log(pets.includes('cat'));//预期输出:trueconsole.log(pets.includes('at'));//预期输出:false17.find()方法返回数组中第一个满足提供的测试函数的元素的值。否则返回未定义。constarray1=[5,12,8,130,44];constfound=array1.find(元素=>元素>10);console.log(found);//output:1218.findIndex()方法在满足提供的测试函数的第一个元素的数组索引中返回。如果没有找到相应的元素,则返回-1。constarray1=[5,12,8,130,44];常量isLargeNumber=(元素)=>元素>13;console.log(array1.findIndex(isLargeNumber));//预期输出:319.toString()返回表示指定数组及其元素的字符串。constarray1=[1,2,'a','1a'];console.log(array1.toString());//预期输出:“1,2,a,1a”20.toLocaleString()返回一个字符Strings表示数组中的元素。数组中的元素将使用它们各自的toLocaleString方法转换为字符串,并且这些字符串将由特定于语言环境的字符串(例如逗号“,”)分隔。constarray1=[1,'a',newDate('21Dec199714:12:00UTC')];constlocaleString=array1.toLocaleString('en',{timeZone:'UTC'});console.log(localeString);//预期输出:“1,a,12/21/1997,2:12:00PM”,//这里假定“en”语言环境和UTC时区-你的结果可能会有所不同21.flat()方法将按照指定的深度递归遍历数组,并将遍历的子数组中的所有元素和元素组合成一个新数组并返回。constarr1=[0,1,2,[3,4]];console.log(arr1.flat());//预期输出:[0,1,2,3,4]constarr2=[0,1,2,[[[3,4]]]];console.log(arr2.flat(2));//预期输出:[0,1,2,[3,4]]22.reduce()方法为每个执行你提供的reducer函数(按升序排列)数组中的元素,将其结果聚合为一个返回值。constreducer=(accumulator,currentValue)=>accumulator+currentValue;//1+2+3+4console.log(array1.reduce(reducer));//预期输出:10//5+1+2+3+4console.log(array1.reduce(reducer,5));//预期输出:1523.some(),对数组的每一项运行给定的函数,如果任何一项返回真,则返回真functioncompare(element,index,array){returnelement>10;}[2,5,8,1,4].some(compare);//false[12,5,8,1,4].some(比较);//true24.values()方法返回一个新的ArrayIterator对象,该对象包含数组每个索引的值constarray1=['a','b','c'];constiterator=array1.values();for(constvalueofiterator){console.log(value);}//预期输出:“a”//预期输出:“b”//预期输出:“c”25.every(),foreachAllitems运行给定函数,每一项返回真,则返回真constisBelowThreshold=(currentValue)=>currentValue<40;constarray1=[1,30,39,29,10,13];console.log(array1.every(isBelowThreshold));//预期输出:true26.substring()和substr()一样:如果只写一个参数,两者作用相同:都从当前截取字符串下标直到字符串末尾的字符串片段。substr(startIndex);substring(startIndex);varstr='123456789';console.log(str.substr(2));//"3456789"console.log(str.substring(2));//"3456789"区别:第二个参数substr(startIndex,lenth):第二个参数是截取字符串的长度(a的字符串从起点截取一定长度);substring(startIndex,endIndex):第二个参数是截取的字符串的最终下标(截取2个位置之间的字符串,‘包头不包尾’)。console.log("123456789".substr(2,5));//"34567"console.log("123456789".substring(2,5));//"345"等等,别走,还没点赞关注呢!如果想了解数组的更多方法,建议在MDN上学习。