给定2个数组Array1 = {a,b,c...n},Array2 = {10,20,15....x}如何生成所有可能的组合作为字符串a(i)b(j)c(k)n(p)
其中
1 <= i <= 10, 1 <= j <= 20 , 1 <= k <= 15, .... 1 <= p <= x
Run Code Online (Sandbox Code Playgroud)
如:
a1 b1 c1 .... n1
a1 b1 c1..... n2
......
......
a10 b20 c15 nx (last combination)
Run Code Online (Sandbox Code Playgroud)
所以在所有组合的总数=元素的产品 array2 =
(10 X 20 X 15 X ..X x)
类似于笛卡尔积,其中第二个数组定义第一个数组中每个元素的上限.
固定数字的示例,
Array x = [a,b,c]
Array y = [3,2,4]
Run Code Online (Sandbox Code Playgroud)
所以我们将有3*2*4 = 24种组合.结果应该是:
a1 b1 c1
a1 b1 …Run Code Online (Sandbox Code Playgroud) 如何在N个可变长度的JavaScript数组中生成所有值的组合?
假设我有N个JavaScript数组,例如
var first = ['a', 'b', 'c', 'd'];
var second = ['e'];
var third = ['f', 'g', 'h', 'i', 'j'];
Run Code Online (Sandbox Code Playgroud)
(在这个例子中有三个数组,但它有N个数组用于解决问题.)
我想输出它们的所有值的组合,以产生
aef
aeg
aeh
aei
aej
bef
beg
....
dej
Run Code Online (Sandbox Code Playgroud)
编辑:这是我工作的版本,使用ffriend接受的答案作为基础.
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 …Run Code Online (Sandbox Code Playgroud) 我在编写代码时难以在JavaScript中生成包含m个元素的n个数组的组合.我已经在其他语言中看到了类似的问题,但答案包含语法或库魔法,我不确定如何翻译.
考虑这些数据:
[[0,1], [0,1,2,3], [0,1,2]]
Run Code Online (Sandbox Code Playgroud)
3个数组,其中包含不同数量的元素.我想要做的是通过组合每个数组中的项来获得所有组合.
例如:
0,0,0 // item 0 from array 0, item 0 from array 1, item 0 from array 2
0,0,1
0,0,2
0,1,0
0,1,1
0,1,2
0,2,0
0,2,1
0,2,2
Run Code Online (Sandbox Code Playgroud)
等等.
如果数组的数量是固定的,那么很容易进行硬编码实现.但阵列的数量可能会有所不同:
[[0,1], [0,1]]
[[0,1,3,4], [0,1], [0], [0,1]]
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
我有两个数组:
var array1=["A","B","C"];
var array2=["1","2","3"];
Run Code Online (Sandbox Code Playgroud)
如何设置另一个数组以包含上述的每个组合,以便:
var combos=["A1","A2","A3","B1","B2","B3","C1","C2","C3"];
Run Code Online (Sandbox Code Playgroud) 我正在寻找循环通过一系列选项的最佳方法,以确保我点击所有可用选项.
我创建了一个功能,允许客户端构建基本上是彼此叠加的其他图像的图像.这些其他图像被分成不同的组.它们在图像一侧有链接,可以单击它们滚动浏览所有不同的图像以查看它们.
现在,我正在制作一个自动化流程,该流程将运行在用户单击其中一个链接时更改图像的功能.我需要确保在此过程中击中不同图像的每个可能组合.
假设有3种不同的帽子,4种不同的衬衫,5种不同的裤子和6种不同的鞋子.我可以将其表示为一个数组,其中包含每个组的选项数.目前的数组是[3, 4, 5, 6].
循环遍历此数组以确保显示所有可能选项的最佳方法是什么?
所以我有一个用可变长度数组填充的可变长度数组。例如这样的事情:
var arr2d = [
['red', 'blue'],
['cotton','polyester','silk'],
['large','medium','small']
]
Run Code Online (Sandbox Code Playgroud)
我试图从每个数组中获取一个的所有可能组合。所以答案应该是这样的:
var answer = [
['red', 'cotton', 'large'],
['red', 'cotton', 'medium'],
['red', 'cotton', 'small'],
['red', 'polyester', 'large'],
.
.
.
]
Run Code Online (Sandbox Code Playgroud)
我已经查看了有关此主题的其他答案,但所有答案都在 Java 中(我需要 javascript)并且他们正在寻找所有组合,而不仅限于length === arr2d.length. 我已经看了将近 2 个小时,但仍然想不出递归地执行此操作的方法。这是头部爆炸场景之一,因为两个数组的长度都不同(我有一个这些二维数组的数组,我必须获得组合)。在我列出的示例中,只有 18 种可能性,但实际上可能有数千种。
我一直试图将invRegex.py移植到node.js实现一段时间,但我仍然在努力解决它.由于ret.js标记器,我已经有了正则表达式解析树,并且它工作得很好,但是以一种节省内存的方式实际生成和连接所有不同的元素对我来说是非常具有挑战性的.为了简单起见,我可以说我有以下正则表达式:
[01]{1,2}@[a-f]
Run Code Online (Sandbox Code Playgroud)
提供以invRegex.py产生以下输出(标签化以占用更少的空间):
0@a 0@b 0@c 0@d 0@e 0@f
00@a 00@b 00@c 00@d 00@e 00@f
01@a 01@b 01@c 01@d 01@e 01@f
1@a 1@b 1@c 1@d 1@e 1@f
10@a 10@b 10@c 10@d 10@e 10@f
11@a 11@b 11@c 11@d 11@e 11@f
Run Code Online (Sandbox Code Playgroud)
考虑到我能够获得每个单独的令牌并生成所有有效单个输出的数组:
[01]{1,2} = function () {
return ['0', '00', '01', '1', '10', '11'];
};
@ = function () {
return ['@'];
};
[a-f] = function () {
return ['a', 'b', 'c', 'd', 'e', …Run Code Online (Sandbox Code Playgroud) 我需要根据N个属性列表生成一组完整的变体,同时保持属性名称不变.
var input = [
{ 'colour' : ['red', 'green'] },
{ 'material' : ['cotton', 'wool', 'silk'] },
{ 'shape' : ['round', 'square', 'rectangle'] }
];
var expected = [
{ 'colour': 'red', 'material': 'cotton', 'shape': 'round' },
{ 'colour': 'red', 'material': 'cotton', 'shape': 'square' },
{ 'colour': 'red', 'material': 'cotton', 'shape': 'rectangle' },
{ 'colour': 'red', 'material': 'wool', 'shape': 'round' },
{ 'colour': 'red', 'material': 'wool', 'shape': 'square' },
{ 'colour': 'red', 'material': 'wool', 'shape': 'rectangle' },
{ 'colour': …Run Code Online (Sandbox Code Playgroud) 我有一个以下形式的对象(下面的简化测试用例)
var test = {
shirts: {
sizes: ['large', 'medium']
,colors:['red', 'blue']
}
, trousers: {
type: ['formal', 'casual']
, pattern: ['plaid', 'stripes']
}
};
Run Code Online (Sandbox Code Playgroud)
我想生成属性的笛卡尔积,以便输出是以下形式的数组:
// desired output
[ {shirts:{sizes:'large', color:'red'}, trousers:{type:'formal', pattern:'plaid'}}
,{shirts:{sizes:'large', color:'red'}, trousers:{type:'formal', pattern:'stripes'}}
,{shirts:{sizes:'large', color:'red'}, trousers:{type:'casual', pattern:'plaid'}}
, {shirts:{sizes:'large', color:'red'}, trousers:{type:'casual', pattern:'stripes'}}
,{shirts:{sizes:'large', color:'blue'}, trousers:{type:'formal', pattern:'plaid'}}
..... and so on ]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我编写了以下代码(基于对来自另一个SO帖子的数组的笛卡尔积的代码的修改)但我似乎将自己绑在试图使其工作的结.
function myCartesianProduct(input, current) {
if (!input) { return []; }
var head = input[Object.keys(input)[0]];
var tail = objSlice(input);
var output = [];
for …Run Code Online (Sandbox Code Playgroud) 尝试用C#/ Linq或甚至原始Mongodb查询本身如何将多个数组作为笛卡尔积加入.
比方说,我有一个集合,我过滤到以下两个文件:
[
{"movie":"starwars","showday":"monday"},
{"movie":"batman","showday":"thursday"},
{"movie":"sleepless","showday":"tuesday"}
]
[
{"actor":"angelina","location":"new york"},
{"actor":"jamie","location":"california"},
{"actor":"mcavoy","location":"arizona"}
]
Run Code Online (Sandbox Code Playgroud)
如何连接每个数组中的每个项目以生成以下类型的结果?
[{"movie":"starwars","showday":"monday","actor":"angelina","location":"new york"},
{"movie":"batman","showday":"thursday","actor":"angelina","location":"new york"},
{"movie":"sleepless","showday":"tuesday","actor":"angelina","location":"new york"},
{"movie":"starwars","showday":"monday","actor":"jamie","location":"california"},
{"movie":"batman","showday":"thursday","actor":"jamie","location":"california"},
{"movie":"sleepless","showday":"tuesday","actor":"jamie","location":"california"},
{"movie":"starwars","showday":"monday","actor":"mcavoy","location":"arizona"},
{"movie":"batman","showday":"thursday","actor":"mcavoy","location":"arizona"},
{"movie":"sleepless","showday":"tuesday","actor":"mcavoy","location":"arizona"}]
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个可以处理任意数量文档的解决方案.因此,例如,如果在此示例中有第3个文档,其中还有3个对象数组,它们将在数组中生成27个项目的结果集 - 或者原样为27行.
希望有一个如何使用C#(Linq?)Mongodb驱动程序来查询和返回这样的数据的解决方案,但是对于mongodb特定的查询也是开放的,因为我希望能从那里反转逻辑.谢谢
javascript ×8
arrays ×4
c# ×2
combinations ×2
algorithm ×1
function ×1
generator ×1
iterator ×1
linq ×1
loops ×1
mongodb ×1
node.js ×1
permutation ×1
python ×1
set ×1