JavaScript 二维数组排列组合

<html>
      
    <head>
        <title>二维数组排列组合</title>
    </head>

    <body>
        <div id="showDiv"></div>
    </body>

    <script type="text/javascript">
        var arrays = [ ["a0", "a1", "a2", "a3"], ["b0", "b1", "b2"], ["c0","c1","c2","c3", "c4"] ];
        var array = getArrayByArrays(arrays);

        window.document.getElementById('showDiv').innerHTML = '元素个数:' + array.length + '<br /><br />' + array.join('<br />');
      
          /**
         * 获取【二维数组】的【排列组合】
        */
        function getArrayByArrays(arrays) {
            var arr = [""];        // 初始化第一个内层数组

            /**
             * 遍历外层数组
            */
            for (var index = 0; index < arrays.length; index++) {
                // console.log('外层数组索引 = ' + index);
                arr = getValuesByArray(arr, arrays[index]);
            }

            return arr;
        }
      
        function getValuesByArray(arr1, arr2) {
            var arr = [];

            /**
             * 遍历外层数组
            */
            for (var index = 0; index < arr1.length; index++) {
                var value1 = arr1[index];

                /**
                 * 遍历内层数组
                */
                for(var cursor = 0; cursor < arr2.length; cursor++) {
                    var value2 = arr2[cursor];
                    var value = value1 + ' - ' + value2;

                    arr.push(value);
                    console.log(arr);
                };
            };

            return arr;
        };

    </script>

</html>

 

猜你喜欢

转载自www.cnblogs.com/hapday/p/10548515.html
0条评论
添加一条新回复