JS把数组中相同元素组合成一个新的数组问题
<!DOCTYPE HTML> <html> <head> <script type = "text/javascript"> Array.prototype.remove=function(dx) { if(isNaN(dx)||dx>this.length){return false;} for(var i=0,n=0;i<this.length;i++) { if(this[i]!=this[dx]) { this[n++]=this[i] } } this.length-=1 } var arr=['1','1','1','2','1','','3','4','5','5','6','7','6','8','9','1','7']; newArr = [], trarr = [], kongFl = false, n = 0; var arr1 = []; for(var i in arr){ arr1[i] = arr[i]; } for(var i = 0; i < arr.length; i++) { var index = arr[i]; if(index == ''){ kongFl = true; }else{ kongFl = false; } for(var j = 0,len = arr1.length;j<len;j++){ if(index == arr1[j]){ trarr.push(arr1[j]); arr1.remove(j); } } if(trarr.length!=0 || kongFl){ newArr.push(trarr); trarr = []; } } console.log(newArr) </script> </head> <body> </body> </html>
今天看到一个博客,发现他那个有个问题,自己就写了一下,一起分享一下