首页 > 代码库 > JS数组去重,js中数组(Array)的排序(sort)

JS数组去重,js中数组(Array)的排序(sort)

JS数组去重

 

var ddd = [1,2,4,5,2, 222,1,5,6];var uq = {};var rq = [];for(var i=0; i<9; i++){      if(!uq[ddd[i]]){          uq[ddd[i]] = true;          rq.push(ddd[i]);      }}return rq;

  

js中数组(Array)的排序(sort)注意事项

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