JS multiple (N) array of elements of the combination of sorting algorithm, multi-dimensional arrays of permutations or combinations of permutations between multiple groups

Source: Internet
Author: User

Now there are a number of mobile phones where the color has [' white ', ' black ', ' gold ', ' Pink ']; the memory size has [' 16G ', ' 32G ', ' 64G ', ' 128G '], the version has [' Mobile ', ' Unicom ', ' telecom '], requires writing an algorithm that implements [[' White ' , ' move ', [' White ', ' 16G ', ' unicom ' ... ] Such a combination, expansion, if there are parameters, such as adding a [' country line ', ' Port Version ', ' American version '], do not change the program can be executed!

This algorithm can be found by the above law: An array contains several arrays, which are combined

Algorithm code one:

//functions that perform a combination arrangement    functionDoexchange (array) {varLen =arr.length; //when the array is greater than or equal to 2        if(Len >= 2){            //the length of the first array            varLen1 = arr[0].length; //the length of the second array            varLen2 = arr[1].length; //number of combinations produced by 2 arrays            varLenboth = Len1 *Len2; //declare a new array, do data staging            varItems =NewArray (Lenboth); //declare the index of the new array            varindex = 0; //2-layer nested loops, grouped into a new array             for(vari=0; i<len1; i++){                 for(varj=0; j<len2; J + +) {Items[index]= Arr[0][i] + "|" + arr[1][j]; Index++; }            }            //adds the newly assembled array to the original array            varNEWARR =NewArray (len-1);  for(vari=2;i<arr.length;i++) {Newarr[i-1] =Arr[i]; } newarr[0] =items; //Execute callback            returnDoexchange (NEWARR); }Else{            returnArr[0]; }    }    //Execution    varArray = [[' A ', ' B ', ' C '], [1, 2, 3], [' X ', ' y ', ' z ']]; vararr1 = [[' A ', ' B ', ' C ']];Console.log (Doexchange (array));

Two:

functionDoexchange (arr, depth) { for(vari = 0; i < arr[depth].length; i++) {result[depth]=Arr[depth][i]if(Depth! = Arr.length-1) {Doexchange (arr, depth+ 1)        } Else{Results.push (Result.join (|))        }    }}functionTest (arr) {results= []; Result= []; Doexchange (arr,0); Console.log (Results.length, Results.join (‘,‘));} Garr= [[' A ', ' B ', ' C '],[' 1 ', ' 2 ', ' 3 '],[' x ', ' y ', ' z '],]test (Garr)

there may be similar requirements, but what we are going to talk about today is not the need, but the one in which each combination is an array :

/ * Returns an array of combinations */
functionDoexchange (array) {varLen =arr.length; //when the array is greater than or equal to 2 if(Len >= 2){ //the length of the first array varLen1 = arr[0].length; //the length of the second array varLen2 = arr[1].length; //number of combinations produced by 2 arrays varLenboth = Len1 *Len2; //declare a new array varItems =NewArray (Lenboth); //declare the index of the new array varindex = 0; for(vari=0; i<len1; i++){ for(varj=0; j<len2; J + +){ if(Arr[0][i]instanceofArray) {Items[index]= Arr[0][i].concat (arr[1][j]); }Else{Items[index]= [Arr[0][i]].concat (arr[1][j]); } Index++; } } varNEWARR =NewArray (len-1); for(vari=2;i<arr.length;i++) {Newarr[i-1] =Arr[i]; } newarr[0] =items; returnDoexchange (NEWARR); }Else{ returnArr[0]; } } // vararr = [[' A ', ' B ', ' C ', ' d '], [1, 2, 3,4], [' X ', ' y ', ' z '],[' Meizu phone ']];Console.log (Doexchange (arr));

JS multiple (N) array of elements of the combination of sorting algorithm, multi-dimensional arrays of permutations or combinations of permutations between multiple groups

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.