找出五个项目数组中可能的组合总数

时间:2014-12-24 06:31:55

标签: javascript arrays

我有五个巨大的数组,每个数组都填充了字符串,每个数组/列表中都有不同数量的项目 这是列表的一个例子:

List 1

    "Jeffrey the Great",
    "Bean-man",
    "Joe",
    "Charles",
    "Flamur",
    "Leka",
    "the defender of men",

List 2

    "awesome",
    "claustrophobic",
    "sad",
    "very masculine",
    "stinky",
    "outrageous",
    "underage",
    "endangered",
    "filthy",
    "kinda smooth",
    "overly threatening",

List 3

    "corpse",
    "skeleton",
    "average joe",
    "mafia boss",
    "murderer",
    "butcher",
    "dog",
    "muslim",
    "fish salesman",

List 4

    "impregnate",
    "punch",
    "eat",
    "kill",
    "hunt down",
    "outrun",
    "touch",


List 5

    "Steve",
    "Stalin",
    "el Chupacabra",
    "everyone",
    "a donut",
    "a fish",
    "the Americans",
    "your neighbors",

我想找出五个列表的所有可能组合 例如:

Jeffery the Great, awesome, corpse, impregnate, steve
Bean-man, awesome, corpse, impregnate, steve
Joe, awesome, corpse, impregnate, steve
Charles, awesome, corpse, impregnate, steve
Jeffery the Great, claustrophobic, corpse, impregnate, steve
Bean-man, claustrophobic, corpse, impregnate, steve
Joe, claustrophobic, corpse, impregnate, steve
Charles, claustrophobic, corpse, impregnate, steve

...等等每个列表中的每个项目。

每个组合必须包含每个数组/列表中的一个。 我不想生成可能的组合,只有每个可能组合的计数! 例如:

List 1
    a
    b
    c

List 2
    1
    2
    3

结果:

9 possible combinations

任何线索我怎么能找到这个?

1 个答案:

答案 0 :(得分:1)

简单的排列和组合数学和javascript

var num_of_combination = list1.length * list2.length * list3.length * list4.length * list5.length;