js数组随机排列组合

js数组随机排列组合

function combine(i, results, res) {//排列组合
                    var wordList = [[1,2],[3,4]]
                        if(wordList.length === 0){
                            results = []
                        }else{
                            for (var j = 0; j < wordList[i].length; j++) {
                                var temp = res;
                                if (i < wordList.length - 1) {
                                    res += JSON.stringify(wordList[i][j]) + ','
                                } else {
                                    res += JSON.stringify(wordList[i][j])
                                }

                                if (wordList[i + 1]) combine(i + 1, results, res);
                                else {
                                    res = JSON.parse('[' + res + ']')
                                    results.push(res);
                                }
                                res = temp;
                            }
                        }
                    }
                    var results = [];
                    let res_obj = []
                    combine(0, results, res_obj);
                    console.log(results )

结果 :[[1, 3],[1, 4], [2, 3], [2, 4]]

    原文作者:全赣州最老实的男人
    原文地址: https://blog.csdn.net/weixin_43827751/article/details/111353903
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞