IndexOf implements the method of the array de-weight, there are three versions, a JS version, 2 augular versions

Source: Internet
Author: User

First JS simplified version of the array to go heavy, need m layer control data

Aunt

1 varAA = [1,3,4,5,6,23,523,6,123, ' 1 ', 12, ' Liu ', ' Liu ', ' Liu ', ' Xiang ', 12,123,214, ' 1 ', ' 3 ', ' Liu Xiang ', ' Liu Xiang '];2         3             functionarr (arr) {4                 varresult = [];5                  for(vari=0;i<arr.length;i++) {6                     if(Result.indexof (arr[i]) = =-1) {7 Result.push (Arr[i])8                     }9                 }Ten Console.log (result); One             } AARR (AA);

A simplified array of angular for the M-Layer control data

<! DOCTYPE html>varM1 = Angular.module (' myApp '),[]); //Create a custom serviceM1.factory (' Myrandom ',function(){                return{show:function(num1,num2) {//1. Each time a repeatable (0-NUM2) random number is generated inow into an array of length NUM1                        vararr = [];  for(vari=0;i<num1;i++){                            varInow = parseint (Math.random () *num2);                        Arr.push (Inow);                        }; //2. Traverse, go to Heavy arr.indexof (subString) returns 1 if subString is not found in arr.                         varresult = [];  for(vari=0;i<arr.length;i++) {                            if(Result.indexof (arr[i]) = =-1) {Result.push (Arr[i])}}; //3. Array sortingResult.sort ((N1,N2) ={                            returnN1-N2;                        }); //4. Return results                        return' Initial array:${arr}<br/>: ${result} ';                    }                }            }); //the invocation of a custom service has three points: 1. The service is already defined 2. The order of dependencies for custom services must be written in the back of the system service 3. No additional $ required for custom service namingM1.controller (' Test ', [' $scope ', ' myrandom ',function($scope, myrandom) {document.write (Myrandom.show (20,20));            }]); </script> </body>

And finally this is the angular version of the V-Layer Control array method

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title> Custom Array de-weight </title>67<body>8<div ng-controller= "Test" >9             Ten<p>1. Random array de-weight </p> OneArray Length: <input type= "number" type= "text" ng-model= "Num1"/> AFetch Range (0,n): <input type= "number" type= "text" ng-model= "num2"/> -<input type= "button" ng-click= "click ()" value= "Generate Array"/> -<p> get array: <span ng-bind= "Arr" ></span></p> the<p> array de-weight: <span ng-bind= "Result" ></span></p> -              - -              +<p>2. Assigning an array to weight </p> -Input array: <input type= "text" ng-model= "str"/> (input format: 1,1,2,2,3,3, up, up) Note: please use commas +<input type= "button" ng-click= "Click2 ()" value= "Array de-weight"/> A<p> array de-weight: <span ng-bind= "RESULT2" ></span></p> at</div> -<script type= "Text/javascript" src= "Angular.min.js" ></script> -<script> -             varM1 = Angular.module (' myApp '),[]); -             //random array de-weight -M1.factory (' Myarr ', () ={ in                 return{ -Show: (num1,num2) ={ to                         //each time a repeatable (0-NUM2) random number is generated inow into an array of length NUM1 +                         vararr = []; -                          for(vari=0;i<num1;i++){ the                             varInow = parseint (Math.random () *num2); * Arr.push (inow); $                         };Panax Notoginseng                         returnarr; -                     } the                 } +             }); AM1.factory (' Myresult ', () ={ the                 return{ +                     //Traverse, go to Heavy arr.indexof (subString) returns 1 if no subString is found in arr.  -Norepeat: (arr) = ={ $                         varresult = []; $                          for(vari=0;i<arr.length;i++) { -                             if(Result.indexof (arr[i]) = =-1) { - Result.push (Arr[i]) the                             } -                         };Wuyi                         //Array Sorting theResult.sort ((N1,N2) ={ -                             returnN1-N2; Wu                         }) -                         returnresult; About                     } $                 } -             }); -             //specifies the array to be weighed -M1.factory (' Fixarr ', () ={ A                 return{ +Remove: (str) = ={                     the                             varARR2 = Str.split (', '); -                             varresult = []; $                             varA =arr2.length; the                              for(vari=0;i<a;i++) { the                                 if(Result.indexof (arr2[i]) = =-1) { the Result.push (Arr2[i]) the                                 } -                             } in                             returnresult; the                     } the                 } About             }); theM1.controller (' Test ', [' $scope ', ' Myarr ', ' myresult ', ' Fixarr ', ($scope, Myarr,myresult,fixarr) ={ the                 //get random numbers and ranges the$scope. NUM1 = "; +$scope. num2 = ";  -                 //random array de-weight the$scope. Click = () ={Bayi                     if($scope. Num1 = = "&& $scope. num2 = ="){ the$scope. arr = ' Please enter length and range '; the}Else{ -$scope. arr =myarr.show ($scope. NUM1, $scope. num2); -$scope. result =myresult.norepeat ($scope. arr); the                     } the                 }; the                 //fixed array de-weight the$scope. str = "; -$scope. Click2 = () ={ the                     if($scope. str = = "){ the$scope. result2 = ' Please input array '; the}Else{94$scope. result2 =fixarr.remove ($scope. str); the                     }             the                 }; the             }]); 98  About</script> -         101</body>102

IndexOf implements the method of the array de-weight, there are three versions, a JS version, 2 augular versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.