Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得具有特定序列的数组组合? var combiArray=[“a”、“b”、“c”]; var结果=[]; 对于(var i=0;i_Javascript_Arrays_Combinations - Fatal编程技术网

Javascript 如何获得具有特定序列的数组组合? var combiArray=[“a”、“b”、“c”]; var结果=[]; 对于(var i=0;i

Javascript 如何获得具有特定序列的数组组合? var combiArray=[“a”、“b”、“c”]; var结果=[]; 对于(var i=0;i,javascript,arrays,combinations,Javascript,Arrays,Combinations,您需要的是所有组合的排列 下面是创建所有组合的代码: var combiArray = ["a", "b", "c"]; var result = []; for(var i =0 ; i<combiArray.length;i++){ result.push(combiArray[i]) for(var b =0 ; b<combiArray.length;b++){ if(i!=b){ result.push(comb

您需要的是所有
组合的排列

下面是创建所有组合的代码:

var combiArray =  ["a", "b", "c"];

var result = [];

for(var i =0 ; i<combiArray.length;i++){
    result.push(combiArray[i])
    for(var b =0 ; b<combiArray.length;b++){
        if(i!=b){
            result.push(combiArray[i]+" "+combiArray[b])
        }

    }
}

//MY OUTPUT: 
[ 'a', 'a b', 'a c', 'b', 'b a', 'b c', 'c', 'c a', 'c b' ]

//WHAT I WANT IS THIS SEQUENCE
[
'a',
'a b',
'a b c',
'a c',
'a c b',
'b',
'b a',    
'b a c',
'b c',    
'b c a',    
'c',
'c a',
'c a b',
'c b',
'c b a',
]
功能组合(arr){
设res=[],len=Math.pow(2,arr.length),c,b,com;
对于(c=1;cb[idx]=“1”);
res.push(com.join)(“”);
}
返回res;
}
行动中

功能组合(arr){
设res=[],len=Math.pow(2,arr.length),c,b,com;
对于(c=1;cb[idx]=“1”);
res.push(com.join)(“”);
}
返回res;
}
日志(组合([“a”、“b”、“c”]);

console.log(组合([“a”,“b”,“c”,“d”]);
@LucaKiebel它不是你共享的链接的副本。你能解释一下你是如何期待这个特殊的序列的吗?
a c
是如何在
a b c
之后出现的吗?
a b c
?杰出的兄弟……我已经被困了一天了……你刚刚解决了它。aarigato
function combination(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com;
    for(c=1;c<len;c++) {
        b = c.toString(2).padStart(arr.length, "0").split("");
        com = arr.filter((_, idx)=>b[idx]==="1");
        res.push(com.join(" "));
    }
    return res;
    }