您将如何在JavaScript中实现多个数组的笛卡尔积?
举个例子,
cartesian([1, 2], [10, 20], [100, 200, 300])
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 cubes[];
for (i in cubes){
cubes[i].dimension
cubes[i].position_x
ecc..
}
Run Code Online (Sandbox Code Playgroud)
现在,让我们假设cubes []以这种方式声明
var cubes[][];
Run Code Online (Sandbox Code Playgroud)
我可以在Javascript中执行此操作吗?我怎样才能自动迭代
cubes[0][0]
cubes[0][1]
cubes[0][2]
cubes[1][0]
cubes[1][1]
cubes[1][2]
cubes[2][0]
ecc...
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,我可以声明:
var cubes[];
var cubes1[];
Run Code Online (Sandbox Code Playgroud)
并与两个数组分开工作.这是更好的解决方案吗?
我不确定如何以简洁的方式提出我的问题,所以我将从示例开始并从那里扩展.我正在使用VBA,但我认为这个问题是非语言特定的,只需要一个可以提供伪代码框架的聪明头脑.在此先感谢您的帮助!
示例:我有3个字符数组,如此:
Arr_1 = [X,Y,Z]
Arr_2 = [A,B]
Arr_3 = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
我想生成所有可能的字符数组排列,如下所示:
XA1
XA2
XA3
XA4
XB1
XB2
XB3
XB4
YA1
YA2
.
.
.
ZB3
ZB4
Run Code Online (Sandbox Code Playgroud)
这可以使用3 while循环或for循环轻松解决.我的问题是,如果数组的数量未知且每个数组的长度未知,我该如何解决这个问题?
所以作为4个字符数组的例子:
Arr_1 = [X,Y,Z]
Arr_2 = [A,B]
Arr_3 = [1,2,3,4]
Arr_4 = [a,b]
Run Code Online (Sandbox Code Playgroud)
我需要生成:
XA1a
XA1b
XA2a
XA2b
XA3a
XA3b
XA4a
XA4b
.
.
.
ZB4a
ZB4b
Run Code Online (Sandbox Code Playgroud)
所以广义的例子是:
Arr_1 = [...]
Arr_2 = [...]
Arr_3 = [...]
.
.
.
Arr_x = [...]
Run Code Online (Sandbox Code Playgroud)
有没有办法构造一个函数,它将生成一个未知数量的循环并循环遍历每个数组的长度以生成排列?或者也许有更好的方法来思考这个问题?
感谢大家!
arrays computer-science arraylist permutation multidimensional-array
我有以下JavaScript对象结构:
var options = {
optionOne: [true, false],
optionTwo: [true, false],
optionThree: [
null,
{property1: 9, property2: 7},
{property1: 4, property2: 12},
{property1: 16, property2: 14}
]
};
Run Code Online (Sandbox Code Playgroud)
请注意,此对象中的键/对数量会有所不同.因此,有可能实际上是optionFour,optionFive等,以及每个选项可以有它的数组值的任何数量或类型.
我需要迭代这个对象并创建一个包含所有可能选项组合的对象的数组:
[
{optionOne: true, optionTwo, true, optionThree: null},
{optionOne: false, optionTwo, true, optionThree: null},
{optionOne: true, optionTwo, false, optionThree: null},
{optionOne: false, optionTwo, false, optionThree: null},
{optionOne: true, optionTwo, true, optionThree: {property1: 9, property2: 7}},
{optionOne: false, optionTwo, true, optionThree: {property1: 9, property2: 7}},
{optionOne: true, optionTwo, …Run Code Online (Sandbox Code Playgroud) 尽管阅读了很多关于排列/组合的Q/A:查找JavaScript数组值的所有组合 + JavaScript - 从具有m个元素的n个数组生成组合我还没有找到正确的方法来获得我正在寻找的那种结果.我有一个10值数组:
var arr = [0,1,2,3,4,5,6,7,8,9];
Run Code Online (Sandbox Code Playgroud)
如果我是对的,所有可能的唯一值的置换数组(没有重复)的数量:
[5,9,1,8,2,6,7,0,4,3] [4,8,0,2,1,9,7,3,6,5] ...
Run Code Online (Sandbox Code Playgroud)
是2x3x4x5x6x7x8x9x10 = 3628800
我正在尝试生成一个动态创建'n'数组的函数.例如:
function createArray(0) -> [0,1,2,3,4,5,6,7,8,9]
function createArray(45648) -> [0,1,5,3,2,8,7,9,6] (something like...)
function createArray(3628800) -> [9,8,7,6,5,4,3,2,1,0]
Run Code Online (Sandbox Code Playgroud)
我想要实现它的方式是:
createArray(1)置换最后2个符号(8,9 - > 9,8)
createArray(2-> 6)置换最后3个符号(8,7,9 - > 9,8,7)
createArray(3628800):所有值都被置换(9-> 0)
你认为这可行/容易吗?如果是的话怎么办?
[编辑]
谢谢你的回答
function permute(permutation, val) {
var length = permutation.length,
result = [permutation.slice()],
c = new Array(length).fill(0),
i = 1, k, p,
n = 0;
while (i < length) {
if (c[i] < i) …Run Code Online (Sandbox Code Playgroud) 假设我们有这样一个数组:myArray = [A,A,B,B,C,C,D,E]
我想创建一个算法,以便它找到所有组合,这些组合加起来整个数组,其中没有重复的元素.
示例组合:
[A, B, C, D, E] [A, B, C]
[A, B, C, D] [A, B, C, E]
[A, B, C] [A, B, C] [D, E]
Run Code Online (Sandbox Code Playgroud)
澄清:[A, B, C] [A, B, C] [D, E]并且[A, B, C] [D, E] [A, B, C]是相同的组合.此外,子集的排序也无关紧要.例如[A,B,C],[B,A,C]应该是相同的.到目前为止,我没有超越
var myArray = ["A", "A", "B", "B", "C", "C", "D", "E"]
console.log([...new Set(myArray)])Run Code Online (Sandbox Code Playgroud)
但这根本没有帮助,它只返回一个不同的集合.我之前找不到类似的问题,所以有人可以在这里指导我如何实现这个目标吗?
需要数组的所有可能组合,包括组合的逆向。
例如:
var b = ['a1','b1','a','b'];
Run Code Online (Sandbox Code Playgroud)
需要组合为:
a1,b1,a,b
a1b1,a1a,a1b, b1a1,b1a,b1b, ......,
a1b1a,a1b1b,a1ab1,a1bb1,........,
a1b1ab,a1b1ba.....bab1a1
Run Code Online (Sandbox Code Playgroud)
所有 64 种组合(如果数组有 4 个元素)。我使用 ArrayList 和 Collection API 在 java 中找到了解决方案,但现在我需要一个纯 JavaScript ES5 解决方案。
我尝试了以下方法,但它只提供了较少的组合。
function getCombinations(chars) {
var result = [];
var f = function (prefix, chars) {
for (var i = 0; i < chars.length; i++) {
result.push(prefix + chars[i]);
f(prefix + chars[i], chars.slice(i + 1));
}
}
f('', chars);
return result;
}
Run Code Online (Sandbox Code Playgroud) 从昨天开始,我一直在努力实现这一目标,尽管还没有运气。我找到了解决方案,其中我想要完成的事情总是略有不同。
我试图获得所有可能的组合,有点像这样:combination_k,但我也希望相同的项目与自己配对,因此给出以下内容:
输入[1, 4, 5]和2(组合数)应返回:
[1, 1], [1, 4], [1, 5], [4, 4], [4, 5], [5, 5]
输入[1, 4, 5]并3应返回:
[1, 1, 1], [1, 1, 4], [1, 1, 5], [1, 4, 4], [1, 4, 5], [4, 4, 4], [4, 4, 5], [5, 5, 5], [5, 5, 4], [5, 5, 1] (顺序不重要)。
我一直在调整combination_k,它让我足够用2来工作,但是当我提供3作为参数时它不起作用。
const combinations = getAllCombinations([1, 4, 5], 2);
// combinations = [1, 1], [1, 4], [1, 5], [4, 4], …Run Code Online (Sandbox Code Playgroud) 我在 js 中有以下数组:
var list = [
['nice', 'delicious', 'red'],
['big', 'tiny'],
['apple']
];
Run Code Online (Sandbox Code Playgroud)
我想获得所有可能的变化,例如:
['nice', 'big', 'apple']
['delicious', 'big', 'apple']
['red', 'big', 'apple']
['nice', 'tiny', 'apple']
...
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最佳/最雄辩的方法是什么?
我想找到 n-array 的 options-property 的所有组合。在示例中,数组的长度为 3,但该函数也应该使用更大或更小的数组大小。
var arr = [{
name: 'Fruit',
options: ['apple', 'kiwi']
}, {
name: 'Food',
options: ['bread', 'rice']
}, {
name: 'Drink',
options: ['water', 'cola']
}]
Run Code Online (Sandbox Code Playgroud)
结果应结果打印以下语句
Fruit: apple | Food: bread | Drink: water
Fruit: apple | Food: bread | Drink: cola
Fruit: apple | Food: rice | Drink: water
Fruit: apple | Food: rice | Drink: cola
Fruit: kiwi | Food: bread | Drink: water
Fruit: kiwi | Food: bread | Drink: cola
Fruit: …Run Code Online (Sandbox Code Playgroud) javascript ×10
arrays ×5
algorithm ×4
combinations ×4
permutation ×4
arraylist ×1
loops ×1
recursion ×1