简体   繁体   English

以特定顺序列出所有可能的数组值组合

[英]List all possible array value combinations in specific order

I am quite fresh with programming and I am working on some code to generate various values for my thesis. 我对编程非常了解,并且正在研究一些代码来为我的论文生成各种值。 Having spent several days searching for an answer to my problem without much success, I am hoping this community might be able to provide some insight. 在花了几天时间寻找我的问题的答案后,并没有取得太大的成功,我希望这个社区能够提供一些见识。

Short version : Make the code below independent of the input array arr 's length. 简短版本 :使下面的代码与输入数组arr的长度无关。

Longer version: 较长版本:

I have an array of arrays, arr = [a, b, c, d] , as input to a function where I would like to calculate all possible combinations (ie a1,b1,c1,d1 , a2,b1,c1,d1 , etc.) without having to know the definite number of elements in arr . 我有一个数组arr = [a, b, c, d] ,作为函数的输入,我想计算所有可能的组合(即a1,b1,c1,d1a2,b1,c1,d1等),而不必知道arr中元素的确定数量。

The problem is that the results are dependent on each element's position in arr . 问题在于结果取决于 arr每个元素的位置。 In other words, the name of each array in arr (a, b, c, d) has to be able to be referenced in the results. 换句话说,必须在结果中引用arr (a,b,c,d)中每个数组的名称。 For clarity's sake, say that arr=[oil_price, countries, vessel_types] . 为了清楚起见,请说arr=[oil_price, countries, vessel_types] Then, it wouldn't make much sense if the results came out randomized, and not in the same order, such as: 然后,如果结果随机出现且顺序不同,则没有太大意义,例如:

[ country[1], oil_price[1], vessel_type[1] ] (first pass) [ country[1], oil_price[1], vessel_type[1] ] (首次通过)

[ vessel_type[2], country[1], oil_price[1] ] (second pass) [ vessel_type[2], country[1], oil_price[1] ] (第二遍)

The code below does take the element position in arr into account and creates the resultspace I would like, but requires a for loop for each element, which is what I am trying to avoid as the number of elements in arr could vary from time to time. 下面的代码确实考虑了arr的元素位置并创建了我想要的结果空间,但是每个元素都需要一个for循环,这是我要避免的事情,因为arr的元素数量可能会不时变化。

function resultspace(arr){
results = [];
e_id = 1;
for (count_1 = 0; count_1 < arr[0].length; count_1++){
    for (count_2 = 0; count_2 < arr[1].length; count_2++){
        for (count_3 = 0; count_3 < arr[2].length; count_3++){
            for (count_4 = 0; count_4 < arr[3].length; count_4++){
                for (count_5 = 0; count_5 < arr[4].length; count_5++){
                    for (count_6 = 0; count_6 < arr[5].length; count_6++){
                        id = e_id
                        var1 = arr[0][count_1].value;
                        var2 = arr[1][count_2].value;
                        var3 = arr[2][count_3].value;
                        var4 = arr[3][count_4].value;
                        var5 = arr[4][count_5].value;
                        var6 = arr[5][count_6].value;

                        result_instance = {id:e_id,
                                            VAR1: var1, 
                                            VAR2: var2, 
                                            VAR3: var3, 
                                            VAR4: var4, 
                                            VAR5: var5, 
                                            VAR6: var6
                                            };
                        results.push(result_instance);
                        e_id++;
                    };
                };
            };
        };
    };
return(results);
};
};

I have looked into recursive combination generation here and here , but I haven't been able to take the element position of the input array into account using these methods. 我在这里这里研究了递归组合生成,但是我无法使用这些方法来考虑输入数组的元素位置。

Thank you for reading. 感谢您的阅读。

  1. You want names so you need to use Map instead of Array. 您需要名称,因此需要使用Map而不是Array。
  2. You need to provide names as Array since the order of for-in loops is not guaranteed. 由于无法保证for-in循环的顺序,因此需要提供名称为Array。

Working demo http://jsfiddle.net/tarabyte/VLj7J/ . 工作演示http://jsfiddle.net/tarabyte/VLj7J/ Works in modern browsers. 在现代浏览器中工作。

function permutate(arrays, keys, memo) {
    var options, name;

    if(keys.length ) {
        name = keys.shift();
        options = arrays[name];
        memo = memo || [{}];
        return permutate(arrays, keys, options.reduce(function(all, item){
            return all.concat(memo.map(function(curr){
                var result = copy({}, curr);
                result[name] = item;
                return result;
            }));
        }, []));
    }

    return memo;
}

Call it as follows 如下调用

permutate({name: [], age: [], gender: []}, ['age', 'name', 'gender']);

Function copy 功能copy

function copy(to, from) {
    Object.keys(from).forEach(function(key){
        to[key] = from[key];
    });
    return to;
}

Or an array of {name: []} pairs with additional preprocessing http://jsfiddle.net/tarabyte/VLj7J/2/ . {name: []}数组与其他预处理http://jsfiddle.net/tarabyte/VLj7J/2/配对。

function permutate(data, memo) {
    var options, name;

    if(data.length ) {
        options = data.shift();
        for(name in options){
            options = options[name];
        }
        memo = memo || [{}];
        return permutate(data, options.reduce(function(all, item){
            return all.concat(memo.map(function(curr){
                var result = copy({}, curr);
                result[name] = item;
                return result;
            }));
        }, []));
    }

    return memo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM