简体   繁体   English

如何获得数组中元素的所有可能组合,包括顺序和长度

[英]How to get all possible combinations of elements in an array including order and lengths

 function getAllCombinations(arr) { var f = function(arr) { var result = []; var temp = []; for (var i = 0; i < arr.length; i++) { temp = []; temp.push(arr[i]); result.push(temp); for (var j = 0; j < arr.length; j++) { if (j != i) { temp = []; temp.push(arr[i]); temp.push(arr[j]); result.push(temp); for (var k = 0; k < arr.length; k++) { if (k != i && k != j) { temp = []; temp.push(arr[i]); temp.push(arr[j]); temp.push(arr[k]); result.push(temp); for (var l = 0; l < arr.length; l++) { if (l != i && l != j && l != k) { temp = []; temp.push(arr[i]); temp.push(arr[j]); temp.push(arr[k]); temp.push(arr[l]); result.push(temp); } } } } } } } return result; } return f(arr); } //call this function console.log(getAllCombinations(["a", "b", "c", "d"])); 

 [["a"],["a","b"],["a","b","c"],["a","b","c","d"],["a","b","d"],["a","b","d","c"],["a","c"],["a","c","b"],["a","c","b","d"],["a","c","d"],["a","c","d","b"],["a","d"],["a","d","b"],["a","d","b","c"],["a","d","c"],["a","d","c","b"],["b"],["b","a"],["b","a","c"],["b","a","c","d"],["b","a","d"],["b","a","d","c"],["b","c"],["b","c","a"],["b","c","a","d"],["b","c","d"],["b","c","d","a"],["b","d"],["b","d","a"],["b","d","a","c"],["b","d","c"],["b","d","c","a"],["c"],["c","a"],["c","a","b"],["c","a","b","d"],["c","a","d"],["c","a","d","b"],["c","b"],["c","b","a"],["c","b","a","d"],["c","b","d"],["c","b","d","a"],["c","d"],["c","d","a"],["c","d","a","b"],["c","d","b"],["c","d","b","a"],["d"],["d","a"],["d","a","b"],["d","a","b","c"],["d","a","c"],["d","a","c","b"],["d","b"],["d","b","a"],["d","b","a","c"],["d","b","c"],["d","b","c","a"],["d","c"],["d","c","a"],["d","c","a","b"],["d","c","b"],["d","c","b","a"]] 

A total of 64 combinations for a 4 length array. 4个长度数组的总共64个组合。

The function works fine but I need to make this function recursive. 该函数工作正常,但我需要使该函数递归。 The for loops have to be nested based on the length of the array and the push also increased per nested loop. for循环必须基于数组的长度进行嵌套,并且每个嵌套循环的推送也将增加。

Really appreciate some advice. 真的很感谢一些建议。

Here is my solution using a subroutine , and closures . 这是我使用子例程闭包的解决方案。 Also slice is very useful here. 切片在这里也非常有用。

If you found this helpful, or if you think other people will find this helpful, don't be afraid to upvote. 如果您认为这有帮助,或者您认为其他人也会对此有帮助,请不要害怕投票。

 function getMyCombinations(coll) { const result = []; (function search(currentPerm, letters) { if (letters.length === 0) return result.push(currentPerm); let trimArray = letters.slice(1); letters.forEach(letter => search(currentPerm + letter, trimArray)); })('', coll) return result; } console.log(getMyCombinations(["a", "b", "c", "d"])); 

I have refactored my original answer to better align with the users request. 我重构了原始答案,以更好地与用户要求保持一致。

 function findPerm(array, currentPerm = '', result =[]) { if (array.length === 0) return result; let trimArray = array.slice(1); array.forEach(v => { let copy = [...result]; let perm = (currentPerm + v).split(''); let res = copy.push(perm); result = findPerm(trimArray, currentPerm + v, copy); }); return result; }; console.log(findPerm(['a', 'b', 'c', 'd'])); 

Finally made it recursive !! 终于使它递归了! Tried to work down on the the original code posted above moving each loop functionality into simple functions. 试图处理上面发布的原始代码,将每个循环功能转换为简单功能。

 function getAllCombinations(inputArray) { var resultArray = []; var combine = function() { for (var i in inputArray) { var temp = []; var tempResult = []; for (var j in arguments) { tempResult.push(inputArray[arguments[j]]); if (arguments[j] == i) { temp = false; } else if (temp) { temp.push(arguments[j]); } } if (temp) { temp.push(i); combine.apply(null, temp); } } if (tempResult.length > 0) { resultArray.push(tempResult); } return resultArray; }; return combine(); } 

See the older version here . 这里查看旧版本。

Result produces 64 unique combinations for a 4 dimensional array 结果为4维数组生成64个唯一组合

 console.log(getAllCombinations(["a", "b", "c", "d"])); 

 [["a","b","c","d"],["a","b","c"],["a","b","d","c"],["a","b","d"],["a","b"],["a","c","b","d"],["a","c","b"],["a","c","d","b"],["a","c","d"],["a","c"],["a","d","b","c"],["a","d","b"],["a","d","c","b"],["a","d","c"],["a","d"],["a"],["b","a","c","d"],["b","a","c"],["b","a","d","c"],["b","a","d"],["b","a"],["b","c","a","d"],["b","c","a"],["b","c","d","a"],["b","c","d"],["b","c"],["b","d","a","c"],["b","d","a"],["b","d","c","a"],["b","d","c"],["b","d"],["b"],["c","a","b","d"],["c","a","b"],["c","a","d","b"],["c","a","d"],["c","a"],["c","b","a","d"],["c","b","a"],["c","b","d","a"],["c","b","d"],["c","b"],["c","d","a","b"],["c","d","a"],["c","d","b","a"],["c","d","b"],["c","d"],["c"],["d","a","b","c"],["d","a","b"],["d","a","c","b"],["d","a","c"],["d","a"],["d","b","a","c"],["d","b","a"],["d","b","c","a"],["d","b","c"],["d","b"],["d","c","a","b"],["d","c","a"],["d","c","b","a"],["d","c","b"],["d","c"],["d"]] 

An alternative solution, seems getting the desired output :) 一种替代解决方案,似乎获得了所需的输出:)

console.log(JSON.stringify(getMyCombinations(["a", "b", "c", "d"])))

function getMyCombinations(arry) {
    var len = arry.length;
    var tempArray = [];
    var result = []
    var tempCount = 1;

    var createCombinations = function(count){
        var singleLevelArray = [];

        arry.forEach(function(item){

            if(count){//1
                if(count > 1){
                    for(var j = 0; j < tempArray.length; j++){
                        if(tempArray[j].indexOf(item) === -1){
                            var x = tempArray[j].slice();
                            x.push(item);
                            singleLevelArray.push(x);
                            result.push(x);
                        }
                    } 
                } else {
                    for(var k = 0; k < len; k++){
                        if(item.indexOf(arry[k]) === -1){
                            tempArray.push([item, arry[k]]);
                            result.push([item, arry[k]]);
                        }
                    }
                }

            } else {
                result.push([item]);
            }

        })
        if(singleLevelArray.length){
            tempArray = []
            tempArray = singleLevelArray;
        }
        if(tempCount < len){
            createCombinations(tempCount++);
        }

        return result;

    }
    return createCombinations()
}

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

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