In the project is often used to some small JS code snippets, some use sometimes their own a bit hesitant to look at the API, some of us to manually write a few demo to find out, the following is I often used in the work of the demo.
About the array:
1, query the duplicate elements in the array, and the number of repetitions
varary = ["1", "33", "22", "1", "1", "22", "22", "33", "22", ' 44 ']; varres = []; Ary.sort (); Console.log (ary); for(vari = 0; I <ary.length;) { varCount = 0; for(varj = i; J < Ary.length; J + +) { if(Ary[i] = =Ary[j]) {Count++; } } if(Count > 1) {Res.push ([ary[i], Count]); } I+=count; } //Res holds the number of repetitions of values and values in a two-dimensional dimension for(vari = 0; i < res.length; i++) {Console.log ("Value:" + res[i][0] + "repetitions:" + res[i][1] + "<br/>"); }
2, use of the sort () method in the array:
Demo1:
varObjectList2 =NewArray (); functionWorkMate (name,age) { This. name=name; var_age=Age ; This. age=function(){ if(!arguments) {_age=arguments[0];} Else { return_age;} }} objectlist2.push (NewWorkMate (' Jack ', 20)); Objectlist2.push (NewWorkMate (' Tony ', 25)); Objectlist2.push (NewWorkMate (' Stone ', 26)); Objectlist2.push (NewWorkMate (' Mandy ', 23)); //sort by age from small to largeObjectlist2.sort (function(A, b) {returnA.age ()-B.age (); }); for(vari=0;i<objectlist2.length;i++) {Document.writeln (' <br/>age: ' +objectlist2[i].age () + ' name: ' +objectlist2[i].name); }
Demo2:
varArrdemo =NewArray (); arrdemo[0] = 10; arrdemo[1] = 50; arrdemo[2] = 51; arrdemo[3] = 100; Arrdemo.sort ();//when the sort method is called, the array itself is changed, which affects the original arrayalert (arrdemo);//10,100,50,51 By default the sort method is sorted in ASCII alphabetical order, not what we think is sorted by number sizeArrdemo.sort (function(A, B) {returnA>b?1:-1});//sort from small to largealert (arrdemo);//10,50,51,100Arrdemo.sort (function(A, B) {returnA<b?1:-1});//sort from big to smallalert (arrdemo);//100,51,50,10
Conclusion:
(1). After the array calls the sort method, it affects itself (rather than generating a new array)
(2) the. Sort () method is sorted by character by default, so when sorting a numeric array, you can't take it for granted that it will be sorted by number size!
(3). To change the default sort behavior (that is, sort by character), you can specify the collation function yourself (as in this example)
3. IndexOf and Splice of arrays
function Testindexofandsplice () { var arr1 = [1, 2, 3, 4, 5]; var index = arr1.indexof (5); var arr = arr1.splice (index,1); Console.log (arr1); Console.log (arr); }
4, the concat in the array
function Testconcat () { var arr1 = [1,2,3,4,5]; var arr2 = [' LW ', ' Jason ', ' TX ']; var arr = arr1.concat (arr2); Console.log (arr); }
About Date:
functiontestdate () {/*var time1 = "1995-11-17"; var date1 = new Date (time1); var date2 = new Date (1995, 11, 20); var difference = Date1-date2; Console.log (difference); Console.log (parseint (Difference/( (((() *) *)));*/ varTime =NewDate (); Time.sethours (19); Time.setminutes (20); vardifference = time-NewDate (); Console.log (difference); Console.log (parseint (Difference/((((24 * 60) * 60) * 1000)))); Console.log (Time.gettime ()-NewDate (). GetTime ()); }
About Regular Expressions:
vartarget = ' Icon_skill '; varTarget1 = ' Icon_wrong '; varpostfix= ' _png '; varstr = ' Icon_skill_haha_nono_fafa_png '; varReg =NewRegExp (' ^ ' +target+ ' [a-za-z\\_]* ' +postfix+ ' $ ')); varREG1 =NewRegExp (' ^ ' +target1+ ' [a-za-z\\_]* ' +postfix+ ' $ ')); varres =reg.exec (str); Console.log (res[0]);//Icon_skill_haha_nono_fafa_pngRes=reg1.exec (str); Console.log (RES); //NULL
About JSON
1, Stringify
functionteststringify () {varARR1 = [1,2,3,4,5]; varARR2 = [' LW ', ' Jason ', ' TX ']; varName = "Jason Li"; varobj = {}; OBJ.ARR1=arr1; OBJ.ARR2=arr2; Obj.name=name; varLW = {}; Lw.name= "LW"; Lw.report=json.stringify (obj); Console.log (obj); Console.log ("Eeee"); Console.log (Json.stringify (LW)); //{"name": "LW", "Report": "{\" arr1\ ": [1,2,3,4,5],\" arr2\ ": [\" lw\ ", \" jason\ ", \" tx\ "],\" name\ ": \" Jason li\ "}"} }
2. Parse
function Testparse () { var str = "{\" result\ ": \" Defender\ ", \" battle_start\ ": [{\" content\ ": [{\" value\ " : {\ "changemodel\": 118},\ "target\": \ "3165150378984059\"}],\ "skill\": \ "model1\", \ "Owner\": \ "3165150378984059\"}, {\ "content\": [{\ "value\": {\ "changemodel\": 118},\ "target\": \ "3164944220554938\"}],\ "skill\": \ "model1\", \ "Owner\ ": \" 3164944220554938\ "}],\" type\ ": \" Arena\ "}"; var strobj = json.parse (str); Console.log (strobj); Console.log ("eeee"); // Console.log (Json.parse (strobj)); }
Frequently used JS Code summary