js多个数组元素两两组合三三组合

var arr1 = ["a","b"];
var arr2 = ["A","B"];
var arr3 = ["1","2"];

想得到 arr = ["a_A","a_B","a_1","a_2","b_A","b_B","b_1","b_2","A_1","A_2","B_1","b_2"];

和 arr = ["a_A_1","a_A_2","a_B_1","a_B_2","b_A_1","b_A_2","b_B_1","b_B_2"];

请问怎么写 ?

阅读 8.5k
4 个回答
cp = arrs => arrs.reduce((a, b) => {
    const arr = [];
    a.forEach(i => {
        b.forEach(j => {
            arr.push(i + "_" + j);
        });
    });
    return arr;
});
console.log(cp([arr1, arr2, arr3]))

我的思路是这样的:
先看成一个组合的问题([x]选n)
再写一个过滤器把含有有同一组元素的组合过滤掉.
这样扩展性会比较好;

//合并数组
function concat(...arrs){
    return [].concat(...arrs);
}

//过滤器 判断是不符合规则
function getFilter(...arrs){
    return function(arr){
        var haveArr = new Set();
        for(let v of arr){
            let perr = arrs.find((a)=>{
                return a.includes(v)
            })
            if(haveArr.has(perr)){
                return false
            }else{
                haveArr.add(perr)
            }
        }
        return true;
    }
}

//递归组合实现
function zuhe(arr,n){
    var pass = new Set();
    var zh = []
    if(n<0 || arr.length < n){throw '参数错误'}
    if(n == 1){
        for(let v of arr){
            if(!pass.has(v)){ //去重
                pass.add(v);
                zh.push([v]);
            }
        }
        return zh;
    }else if(arr.length == n){
        return [arr];
    }
    for(let i=0; i <= arr.length-n; i++ ){
        let v = arr[i];
        if(!pass.has(v)){ //去重
            pass.add(v);
            let _zh = zuhe(arr.slice(i+1),n-1).map(a=>{
                a.unshift(v)
                return a;
            })
            zh = zh.concat(_zh)
        }
    }
    return zh;
}
//----------以上是所需要的工具函数

var arr1 = ["a","b"];
var arr2 = ["A","B"];
var arr3 = ["1","2"];

var allArr = concat(arr1,arr2,arr3); //合并数组
var filter = getFilter(arr1,arr2,arr3); //生成过滤器

var zh1 = zuhe(allArr,2).filter(v=>filter(v)).map(v=>v.join("_")); // x选2>过滤>组装成需要格式
var zh2 = zuhe(allArr,3).filter(v=>filter(v)).map(v=>v.join("_")); // x选3>过滤后>组装成需要格式

console.log(zh1);
console.log(zh2);
/*
["a_A","a_B","a_1","a_2","b_A","b_B","b_1","b_2","A_1","A_2","B_1","B_2"]
["a_A_1","a_A_2","a_B_1","a_B_2","b_A_1","b_A_2","b_B_1","b_B_2"]
*/

---------------- 补充
想明白 其实还是一个组合的问题 只是组合的结果要处理一下 稍微改一下 zuhe 函数:

function zuhe(arr,n){
    var pass = new Set();
    var zh = []
    if(n<0 || arr.length < n){throw '参数错误'}
    if(n == 1){
        for(let _arr of arr){
            for(let v of _arr){
                if(!pass.has(v)){ //去重
                    pass.add(v);
                    zh.push([v]);
                }
            }
        }
        return zh;
    }
    for(let i=0; i <= arr.length-n; i++ ){
        for(let v of arr[i]){
            if(!pass.has(v)){ //去重
                pass.add(v);
                let _zh = zuhe(arr.slice(i+1),n-1).map(a=>{
                    a.unshift(v)
                    return a;
                })
                zh = zh.concat(_zh)
            }
        }
    }
    return zh;
}
var arr1 = ["a","b","c"];
var arr2 = ["A","B"];
var arr3 = ["1","2"];

var zh2 = zuhe([arr1,arr2,arr3],2)
var zh3 = zuhe([arr1,arr2,arr3],3)
console.log(zh2)
console.log(zh3)
            var newArr1 = arr1.map((v,i)=>{
                return arr1[i]+"_"+arr2[i];
            });
            
            var newArr2 = arr1.map((v,i)=>{
                return arr1[i]+"_"+arr2[i]+"_"+arr3[i];
            });

数组个数不限,组合个数不限,可以参考下。

var arr1 = ["a","b"];
var arr2 = ["A","B"];
var arr3 = ["1","2"];



var arrGroup = [arr1,arr2,arr3];
var arrStack = [];
var strStack = [];



function printResult(strStack,arrGroup,index){
    if(index + 1 > arrGroup.length){
         return;
    }
    var arr = arrGroup[index];
    var nextIndex = index + 1;
    for(var i = 0;i < arr.length;i++){
           strStack.push(arr[i]);
           printResult(strStack,arrGroup,nextIndex);
         if(nextIndex + 1   > arrGroup.length){
                  console.log(strStack.join("_"));//打印最终结果。
         }
        if(strStack.length > 0){
              strStack.pop();
        }
    }
}

/**
*  如果二二组合,size = 2 , 如果 三三组合 size = 3
*/
function print(arrStack,strStack,arrGroup,size,index){
    if(index + 1 > arrGroup.length){
          return;
    }
    var nextIndex = index + 1;
    arrStack.push(arrGroup[index]);
    if(arrStack.length == size){
                  printResult(strStack,arrStack,0);
    }
    print(arrStack,strStack,arrGroup,size,nextIndex);

    if(arrStack.length > 0){
           arrStack.pop();
    }
    
    print(arrStack,strStack,arrGroup,size,nextIndex);
}

// 二二组合
print(arrStack,strStack,arrGroup,2,0);
//三三组合
print(arrStack,strStack,arrGroup,3,0);
var arr4 = ["e","f"];
var arr5 = ["g","h"];
arrGroup.push(arr4);
arrGroup.push(arr5);
//四四组合
print(arrStack,strStack,arrGroup,4,0);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题