【问题标题】:All modes to combine two arrays [closed]组合两个数组的所有模式[关闭]
【发布时间】:2018-01-13 21:11:28
【问题描述】:

我在这个结构中有一个动态数组:

$arr = [
    ['R1', 'R2'],
    ['A1', 'A2', 'A3'],
    ['M1', 'M2']
];

我想找到这个数组的所有组合,即:

R1A1M1
R1A1M2
R1A2M1
R1A2M2
R1A3M1
R1A3M2
R2A1M1
R2A1M2
R2A2M1
R2A2M2
R2A3M1
R2A3M2

因为数组长度不是静态的,我们必须使用递归函数来解决这个问题。 解决这个问题的 PHP 或 js 代码是什么?

谢谢

【问题讨论】:

标签: javascript php arrays function recursion


【解决方案1】:

你可以这样做;

var arr = [ ['R1', 'R2'], ['A1', 'A2', 'A3'], ['M1', 'M2']];
    res = arr.reduceRight((a,b) => b.reduce((r,e) => r.concat(a.map(f => e + f)), []));
console.log(res);

【讨论】:

    【解决方案2】:

    你可以在 ES6 中使用单行方法

    var array = [['R1', 'R2'], ['A1', 'A2', 'A3'], ['M1', 'M2']],
        result = array.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
    
    console.log(result.map(a => a.join('')));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:
          var mohem = new Array($arr.reduce((l,a)=>l*a.length,1))
        .fill("");
      
      for(var i = 0; i < mohem.length; i++){
        var l = mohem.length / $arr[0].length;
        for(var j = 0; j < $arr.length; j++){
          mohem[i] += $arr[j][Math.floor(i/l)% $arr[j].length ];
          if($arr[i+1]) l = l / $arr[i+1].length;
        }
      

      【讨论】:

      • 谢谢。您的答案与之前的答案相似。
      【解决方案4】:

      要获取数组的所有组合,您需要一个 Power Set 函数

      在数学中,任何集合 S 的幂集(或幂集),写成 P(S)、℘(S)、P(S)、ℙ(S) 或 2S,是 S 的所有子集的集合, 包括空集和 S 本身。

      您可以自己创建它,或者找到这样一个预先编写好的函数。我将以https://github.com/Xotic750/power-set-x 为例。

      const arr = [
        ['R1', 'R2'],
        ['A1', 'A2', 'A3'],
        ['M1', 'M2']
      ];
      
      const flattened = [...arr[0], ...arr[1], ...arr[2]];
      const combinations = powerSet(flattened);
      const stringCombinations = combinations.map(combination => combination.join(''));
      console.log(stringCombinations);
      <script src="https://rawgit.com/Xotic750/power-set-x/master/lib/power-set-x.min.js"></script>
      <script>
        var powerSet = returnExports
      </script>

      然后您可以对结果执行任何所需的过滤或排序。

      例如,如果您只想要您在问题中给出的组合并按特定顺序。

      const arr = [
        ['R1', 'R2'],
        ['A1', 'A2', 'A3'],
        ['M1', 'M2']
      ];
      
      const flattened = [...arr[0], ...arr[1], ...arr[2]];
      const combinations = powerSet(flattened);
      const stringCombinations = combinations.map(combination => combination.join(''));
      const filtered = stringCombinations.filter(string => /^R\dA\dM\d$/.test(string)).sort();
      console.log(filtered);
      <script src="https://rawgit.com/Xotic750/power-set-x/master/lib/power-set-x.min.js"></script>
      <script>
        var powerSet = returnExports
      </script>

      有关多种语言的 Power Set 功能,请参阅:https://rosettacode.org/wiki/Power_set

      例如,在 ES6 中 Power Set 可以简单地写成

      const arr = [
        ['R1', 'R2'],
        ['A1', 'A2', 'A3'],
        ['M1', 'M2']
      ];
      
      const flattened = [...arr[0], ...arr[1], ...arr[2]];
      const powerSet = xs => xs.reduceRight((a, x) => a.concat(a.map(y => [x].concat(y))), [[]]);
      const combinations = powerSet(flattened);
      console.log(combinations);

      【讨论】:

        【解决方案5】:
         var result = [""];
         $arr.forEach(function(arr){
           var tmp = [];
           arr.forEach(function(el){
            result.forEach(function(curr){
             tmp.push(curr+el);
            });
          });
         result = tmp;
        });
        

        应该做几次迭代......

        Result


        另一种更静态的方法(可能更快):

        var result = new Array($arr.reduce((l,a)=>l*a.length,1))
          .fill("");
        
        for(var i = 0; i < result.length; i++){
          var l = result.length / $arr[0].length;
          for(var j = 0; j < $arr.length; j++){
            result[i] += $arr[j][Math.floor(i/l)% $arr[j].length ];
            if($arr[i+1]) l = l / $arr[i+1].length;
          }
        

        【讨论】:

        猜你喜欢
        • 2020-12-18
        • 1970-01-01
        • 1970-01-01
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多