查找JavaScript数组值的所有组合组合、数组、JavaScript

2023-09-10 22:33:21 作者:难相拥

我如何能产生可变长度的JavaScript数组的N多的所有值的组合?

让我们说我有N多的JavaScript阵列,如:

  VAR第一= ['A','B','C','D'];
VAR秒= ['E'];
VAR第三= ['F','G','H','我','J'];
 

(三个阵列在本实施例中,但其N阵列的问题的数量。)

一行神奇的 javascript 代码

和我想输出自己的价值观的所有组合,产生

  AEF
AEG
AEH
AEI
AEJ
BEF
求
....
德治
 

编辑:这是我得到了工作的版本,使用ffriend的接受的答案为依据

  VAR allArrays = ['A','B'],['C','Z'],['D','E','F'] ;

 功能allPossibleCases(ARR){
  如果(arr.length === 0){
    返回 [];
  }
否则,如果(arr.length === 1){
返回改编[0];
}
其他 {
    VAR的结果= [];
    变种allCasesOfRest = allPossibleCases(arr.slice(1)); //与阵列的其余部分复发
    为(在allCasesOfRest变种C){
      对于(VAR I = 0; I<常用3 [0] .length;我++){
        result.push(ARR [0] [i]于+ allCasesOfRest并[c]);
      }
    }
    返回结果;
  }

}
变种R = allPossibleCases(allArrays);
 //输出[亚洲合作对话,BCD,AZD,BZD,王牌,公元前,阿塞拜疆,BZE,ACF,BCF,AZF,BZF ]
 

解决方案

这是不是排列,见排列的定义从的维基百科。

但是你可以用做到这一点的递归

  VAR allArrays = ['A','B'],['C'],['D','E','F']

功能allPossibleCases(ARR){
  如果(arr.length == 1){
    返回改编[0];
  } 其他 {
    VAR的结果= [];
    变种allCasesOfRest = allPossibleCases(arr.slice(1)); //与阵列的其余部分复发
    对于(VAR I = 0; I< allCasesOfRest.length;我++){
      对于(VAR J = 0; J<常用3 [0] .length; J ++){
        result.push(ARR [0] [J] + allCasesOfRest [I]);
      }
    }
    返回结果;
  }

}
 

您也可以使其与循环,但是这将是一个有点棘手,并且需要实现自己的协议栈的模拟。

How can I produce all of the combinations of the values in N number of JavaScript arrays of variable lengths?

Let's say I have N number of JavaScript arrays, e.g.

var first = ['a', 'b', 'c', 'd'];
var second = ['e'];
var third =  ['f', 'g', 'h', 'i', 'j'];

(Three arrays in this example, but its N number of arrays for the problem.)

And I want to output all the combinations of their values, to produce

aef
aeg
aeh
aei
aej
bef
beg
....
dej

EDIT: Here's the version I got working, using ffriend's accepted answer as the basis.

var allArrays = [['a', 'b'], ['c', 'z'], ['d', 'e', 'f']];

 function allPossibleCases(arr) {
  if (arr.length === 0) {
    return [];
  } 
else if (arr.length ===1){
return arr[0];
}
else {
    var result = [];
    var allCasesOfRest = allPossibleCases(arr.slice(1));  // recur with the rest of array
    for (var c in allCasesOfRest) {
      for (var i = 0; i < arr[0].length; i++) {
        result.push(arr[0][i] + allCasesOfRest[c]);
      }
    }
    return result;
  }

}
var r=allPossibleCases(allArrays);
 //outputs ["acd", "bcd", "azd", "bzd", "ace", "bce", "aze", "bze", "acf", "bcf", "azf", "bzf"]

解决方案

This is not permutations, see permutations definitions from Wikipedia.

But you can achieve this with recursion:

var allArrays = [['a', 'b'], ['c'], ['d', 'e', 'f']]

function allPossibleCases(arr) {
  if (arr.length == 1) {
    return arr[0];
  } else {
    var result = [];
    var allCasesOfRest = allPossibleCases(arr.slice(1));  // recur with the rest of array
    for (var i = 0; i < allCasesOfRest.length; i++) {
      for (var j = 0; j < arr[0].length; j++) {
        result.push(arr[0][j] + allCasesOfRest[i]);
      }
    }
    return result;
  }

}

You can also make it with loops, but it will be a bit tricky and will require implementing your own analogue of stack.