在项目中经常会使用到一些小的js代码段,有些用法有时候自己有点犹豫会查API,有些是要我们自己手动写几个demo来搞清楚的,下面是我经常 在工作中使用到的demo。
关于数组的:
1,查询数组中重复的元素,以及重复的次数
var ary = ["1", "33", "22", "1", "1", "22", "22", "33", "22", ‘44‘]; var res = []; ary.sort(); console.log(ary); for (var i = 0; i < ary.length;) { var count = 0; for (var j = i; j < ary.length; j++) { if (ary[i] == ary[j]) { count++; } } if (count > 1){ res.push([ary[i], count]); } i += count; } //res 二维数维中保存了 值和值的重复数 for (var i = 0; i < res.length; i++) { console.log("值:" + res[i][0] + " 重复次数:" + res[i][1] + "<br/>"); }
2,数组中sort()方法的使用:
demo1:
var objectList2 = new Array(); function WorkMate(name,age){ this.name=name; var _age=age; this.age=function(){ if(!arguments) { _age=arguments[0];} else { return _age;} } } objectList2.push(new WorkMate(‘jack‘,20)); objectList2.push(new WorkMate(‘tony‘,25)); objectList2.push(new WorkMate(‘stone‘,26)); objectList2.push(new WorkMate(‘mandy‘,23)); //按年龄从小到大排序 objectList2.sort(function(a,b){ return a.age()-b.age(); }); for(var i=0;i<objectList2.length;i++){ document.writeln(‘<br />age:‘+objectList2[i].age()+‘ name:‘+objectList2[i].name); }
demo2:
var arrDemo = new Array(); arrDemo[0] = 10; arrDemo[1] = 50; arrDemo[2] = 51; arrDemo[3] = 100; arrDemo.sort(); //调用sort方法后,数组本身会被改变,即影响原数组 alert(arrDemo);//10,100,50,51 默认情况下sort方法是按ascii字母顺序排序的,而非我们认为是按数字大小排序 arrDemo.sort(function(a,b){return a>b?1:-1});//从小到大排序 alert(arrDemo);//10,50,51,100 arrDemo.sort(function(a,b){return a<b?1:-1});//从大到小排序 alert(arrDemo);//100,51,50,10
结论:
(1).数组调用sort方法后,会影响本身(而非生成新数组)
(2).sort()方法默认是按字符来排序的,所以在对数字型数组排序时,不可想当然的以为会按数字大小排序!
(3).要改变默认的sort行为(即按字符排序),可以自行指定排序规则函数(如本例所示)
3、数组的indexOf与splice
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、数组中的concat
function testConcat(){ var arr1 = [1,2,3,4,5]; var arr2 = [‘lw‘,‘Jason‘,‘tx‘]; var arr = arr1.concat(arr2); console.log(arr); }
关于Date:
function testDate() { /*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 / (((24 * 60) * 60) * 1000))));*/ var time = new Date(); time.setHours(19); time.setMinutes(20); var difference = time - new Date(); console.log(difference); console.log( parseInt((difference / (((24 * 60) * 60) * 1000)))); console.log(time.getTime() - new Date().getTime()); }
关于正则表达式:
var target = ‘icon_skill‘; var target1 = ‘icon_wrong‘; var postfix= ‘_png‘; var str = ‘icon_skill_haha_nono_fafa_png‘; var reg = new RegExp(‘^‘+target+‘[a-zA-Z\\_]*‘+postfix+‘$‘); var reg1 = new RegExp(‘^‘+target1+‘[a-zA-Z\\_]*‘+postfix+‘$‘); var res = reg.exec(str); console.log(res[0]); //icon_skill_haha_nono_fafa_png res = reg1.exec(str); console.log(res); //null
关于JSON
1、stringify
function testStringify(){ var arr1 = [1,2,3,4,5]; var arr2 = [‘lw‘,‘Jason‘,‘tx‘]; var name = "Jason Li"; var obj = {}; obj.arr1 = arr1; obj.arr2 = arr2; obj.name = name; var lw = {}; 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)); }
时间: 2024-10-12 01:40:34