JS array to heavy (4 methods)

First type: Dual Circulation

VAR ARY = [1, 2, 3, 3, 2, 3, 4, 5] For (var i = 0; i
  <ary.length;i++){
  for(var j=i+1;j<ary.length;j++){
    if(ary[i]===ary[j]){
      ary.splice(i,1);
      i--;
    }
  }
}
alert(ary); second: new array 

VAR ARY = [1, 2, 3, 3, 2, 3, 4, 5]; var Ary1 = []; for (var i = 0; i

  <ary.length;i++){
 if(ary1.indexOf(ary[i])==-1){
 ary1.push(ary[i]);
 }
}
alert(ary1); Third type: Object to the number of times 

VAR ARY = [1, 2, 3, 3, 2, 3, 4 , 5]; var obj = {}; for (var i = 0; i

 fourth: sort thinking  <ary.length;i++){
  var cur=ary[i];
  if(obj[cur]){
  obj[cur]++;
  }else{
  obj[cur]=1;
  }
 }
 var ary=[];
 for(var attr in obj){
  ary.push(attr);
 }
 alert(ary); 
VAR ARY = [1, 2, 3, 3, 2, 3, 4, 5]; Ary.Sort (Function (A, B) {RETURN AB;}); VAR i = 0; i

The above is all the contents of this article, and it is desirable to bring certain help to everyone’s learning or work, and also hope to support Tumi cloud!

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment