用静态和动态值创建数组组合的最佳方法是什么

时间:2019-09-04 18:55:41

标签: javascript arrays performance combinatorics code-readability

经过长时间的尝试,我设法编写了一个脚本,该脚本创建了数组的所有组合,您可以设置组合的级别。

但是当某些元素保持位置(静态)而周围的其他元素改变(动态)时,如何在组合的限制下进行选择。

这就是我需要的:输出示例:

  min
  "static1", "static2", "static3" // (limited by number of static item if min 3) 
  "static2", "static1", "static3" // if true allow change order of main
  (4) min+1
  ["others1", "static1", "static2", "static3"]
  ["static1", "others1", "static2", "static3"]
  ["others1", "static2", "static1", "static3"]
  ...
  ["others1", "static2", "static1", "static3"]  true // iIf true, then you can allow static values to reorder
  ["others1", "static2", "static1", "static3"]  true
  ...
  (7) min+4
  ["other", "static1", "other1", "static2", "other1", "static3", "other1"] 
  ...
  (9) max+6 
  ["other2", "static1", "other1", "static2", "other2", "other1", "static3", "other3", "other1"]
  ...

我意识到很可能我做错了一切。也许有更好的方法可以做到这一点。 我的算法速度较差,经常出现有关内存不足,周期过多,可读性差的消息... 因此,请您说明如何最好地进行这种组合。

     var static_arr1 = ['a','b','c','d'];
     var static_arr2 = ['a','b','c','d'];
     var combin_arr1 = ['a','b','c','d'];var p= [];var max ="6";
     var a = [static_arr1, static_arr2, combin_arr1]; // OR var a = ['static_arr1':static_arr1,'static_arr2':static_arr2,'combin_arr1':combin_arr1]; if you prefer use multidimension array no problems, convert arrays to array of arrays or function create(){var nam = arguments.name;  return {val: arguments,}}
     
      // ------------------------------------  Count arrays:
     for (var key in window) {var ss = key;
          if( ss.indexOf("tic_") > -1){p.push(key);var min = p.length;  console.log(min);}}   // count global static arrays and set min level of combinations
     
     // =====================================

     function get1DArray(arr){return arr.join().split(",");}                                    // if array multidimensional simple convert to 1d  - lost information and position:( +fast,siple , сheck: if(this.filter(Array.isArray).length > 1){}
     
     Array.prototype.combine=function combine(k){    var toCombine=get1DArray(this);   var last; 
      // ------------------------------------
         function combi(n,comb){var combs=[];
     
             for ( var x=0,y=comb.length;x<y;x++){var v = comb[x];    for ( var l=0,m=toCombine.length;l<m;l++){   
     
                     combs.push(comb[x]+toCombine[l]); 
                 }
             }
     
     
     // recursion
             if (n<k-1){n++;combi(n,combs);}
                                             else{last=combs;}        }
      // ------------------------------------END func
     
         combi(1,toCombine);
         return last;
     }
     
     // ===================================== Example:
     
     for (var i = min; i <= max;i++) { console.log(i)  // get levels from min and max
     var toCombine=[combin_arr1, static_arr2, static_arr1];   
     var results=toCombine.combine(i);
     console.log(results)
      }

0 个答案:

没有答案