使用方便好用的方法来减少代码行数,提高我们的工作效率,增加我们的钓鱼时间。在我们的日常工作中,我们需要编写函数,比如排序、查找、查找唯一值、传递参数、交换值等,所以这里分享一些我在工作中多年积累的常用技巧和方法,so每个人都可以增加他们的经验。鱼的时间。这些方法一定会对你有帮助:减少代码行编码竞赛增加钓鱼时间1.声明并初始化数组我们可以用特定的大小来初始化数组,或者通过指定一个值来初始化数组的内容。你可能会用一组Array,其实二维数组也可以这样做,如下图:constarray=Array(5).fill('');//Output(5)["","","","",""]constmatrix=Array(5).fill(0).map(()=>Array(5).fill(0))//输出(5)[Array(5),数组(5),数组(5),数组(5),数组(5)]0:(5)[0,0,0,0,0]1:(5)[0,0,0,0,0]2:(5)[0,0,0,0,0]3:(5)[0,0,0,0,0]4:(5)[0,0,0,0,0]长度:52。sum,minimumsum我们应该利用reduce方法快速找到基本数学的最大值。constarray=[5,4,7,8,9,2];Sumarray.reduce((a,b)=>a+b);//输出:35个最大值array.reduce((a,b)=>a>b?a:b);//输出:9个最小值valuesarray.reduce((a,b)=>aa-b);//输出(6)[1,5,10,25,40,100]array.sort((a,b)=>b-a);//输出(6)[100,40,25,10,5,1]对象数组排序constobjectArr=[{first_name:'Lazslo',last_name:'Jamf'},{first_name:'Pig',last_name:'Bodine'},{first_name:'Pirate',last_name:'Prentice'}];objectArr.sort((a,b)=>a.last_name.localeCompare(b.last_name));//输出(3)[{…},{…},{…}]0:{first_name:"猪",last_name:"Bodine"}1:{first_name:"Lazslo",last_name:"Jamf"}2:{first_name:"Pirate",last_name:"Prentice"}length:34.从数组中过滤到假值像0,undefined,null,false,"",''可以通过以下技巧轻松过滤掉假值constarray=[3,0,6,7,'',false];array.filter(Boolean);//输出(3)[3,6,7]5.使用逻辑运算符处理需要条件判断的情况functiondoSomething(arg1){arg1=arg1||10;//如果arg1没有值,取默认值10}letfoo=10;foo===10&&doSomething();//如果foo等于10,则执行doSomething();//输出:10foo===5||doSomething();//isthesamethingasif(foo!=5)thendoSomething();//输出:106.去除重复值constarray=[5,4,7,8,9,2,7,5];array.filter((item,idx,arr)=>arr.indexOf(item)===idx);//或constnonUnique=[...newSet(array)];//Output:[5,4,7,8,9,2]7.创建一个计数器对象或者Map大多数情况下,可以统计一些特殊的词通过创建对象或Map的出现频率。letstring='kapilalipak';consttable={};for(letcharofstring){table[char]=table[char]+1||1;}//输出{k:2,a:3,p:2,i:2,l:2}或constcountMap=newMap();for(leti=0;i2,"a"=>3,"p"=>2,"i"=>2,"l"=>2}8.三元运算符很酷functionFever(temp){returntemp>97?'VisitDoctor!':temp<97?'GoOutandPlay!!':temp===97?'TakeSomeRest!';}//输出Fever(97):"TakeSomeRest!"Fever(100):"VisitDoctor!"9、循环方式for和for..in的比较默认是取索引,但是可以用arr[index]代替。for..in也接受非数字,所以避免使用它们。forEach,for...of直接获取元素。forEach也可以得到索引,但是for...of不能。10.合并两个对象constuser={name:'KapilRaghuwanshi',gender:'Male'};constcollege={primary:'ManiPrimarySchool',secondary:'LassSecondarySchool'};constskills={programming:'Extreme',swimming:'Average',sleeping:'Pro'};conssummary={...user,...college,...skills};//合并多个对象gender:"Male"name:"KapilRaghuwanshi"primary:"ManiPrimarySchool"programming:“极限”中学:“LassSecondarySchool”睡觉:“Pro”游泳:“Average”11。箭头函数箭头函数表达式是传统函数表达式的替代方法,但有局限性,不能在所有情况下使用。因为它们有词法作用域(父作用域)并且没有自己的this和参数,所以它们指的是定义它们的环境。constperson={name:'Kapil',sayName(){returnthis.name;}}person.sayName();//输出“Kapil”butthis:constperson={name:'Kapil',sayName:()=>{returnthis.name;}}person.sayName();//Output"13.Optionalchainconstuser={employee:{name:"Kapil"}};user.employee?.name;//Output:"Kapil"user.employ?.name;//Output:undefineduser.employ.name//Output:VM21616:1UncaughtTypeError:Cannotreadproperty'name'ofundefined13.使用内置的Math.random()方法随机排列数组。constlist=[1,2,3,4,5,6,7,8,9];list.sort(()=>{returnMath.random()-0.5;});//输出(9)[2,5,1,6,9,8,4,3,7]//输出(9)[4,1,7,5,3,8,2,9,6]14.双问号语法constfoo=null??'myschool';//Output:"myschool"constbaz=0??42;//Output:0余数和扩展语法functionmyFun(a,b,...manyMoreArgs){returnarguments.length;}myFun("one","two","three","four","five","six");//输出:6constparts=['shoulders','knees'];constlyrics=['head',...parts,'and','toes'];歌词;//输出:(5)["head","shoulders","knees","and","toes"]16.默认参数constsearch=(arr,low=0,high=arr.length-1)=>{returnhigh;}search([1,2,3,4,5]);//输出:417.十进制转二进制或十六进制constnum=10;num.toString(2);//Output:"1010"num.toString(16);//Output:"a"num.toString(8);//Output:"12"18.使用解构交换两个数leta=5;letb=8;[a,b]=[b,a][a,b]//输出(2)[8,5]19.单行回文校验函数checkPalindrome(str){returnstr==str.split('').reverse().join('');}checkPalindrome('nam一个');//输出:true20。将Object属性转换为属性数组constobj={a:1,b:2,c:3};Object.entries(obj);//Output(3)[Array(2),Array(2),Array(2)]0:(2)["a",1]1:(2)["b",2]2:(2)["c",3]长度:3Object.keys(obj);(3)["a","b","c"]Object.values(obj);(3)[1,2,3]~完了,我是小智,我们下期见!作者:KapilRaghuwanshi-283g