码迷,mamicode.com
首页 > 编程语言 > 详细

二维数组 组合方案

时间:2016-05-16 17:05:43      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

例子:

[
  ["160514110637d4800f000005"],
  ["160514110639d4800f000009","160514110639d4800f00000a","160514110639d4800f00000b"],
  ["160514110639d4800f00000d","160514110639d4800f00000e","160514110639d4800f00000f"]
]

方法:

 function doExchange(doubleArrays, fgf) {
            /// <summary>
            /// 二维数组 组合方案
            /// </summary>
            /// <param name="doubleArrays" type="type">二维数组</param>
            /// <param name="fgf" type="type">分隔符</param>
            /// <returns type=""></returns>
            if (fgf == undefined) fgf = ",";
            var len = doubleArrays.length;
            if (len >= 2) {
                var len1 = doubleArrays[0].length;
                var len2 = doubleArrays[1].length;
                var newlen = len1 * len2;
                var temp = new Array(newlen);
                var index = 0;
                for (var i = 0; i < len1; i++) {
                    for (var j = 0; j < len2; j++) {
                        temp[index] = doubleArrays[0][i] + fgf + doubleArrays[1][j];
                        index++;
                    }
                }
                var newArray = new Array(len - 1);
                if (len > 2) {
                    newArray[1] = doubleArrays[2];
                }
                newArray[0] = temp;
                return doExchange(newArray, fgf);
            }
            else {
                return doubleArrays[0];
            }
        }

结果:

[
  "160514110637d4800f000005,160514110639d4800f000009,160514110639d4800f00000d",
  "160514110637d4800f000005,160514110639d4800f000009,160514110639d4800f00000e",
  "160514110637d4800f000005,160514110639d4800f000009,160514110639d4800f00000f",
  "160514110637d4800f000005,160514110639d4800f00000a,160514110639d4800f00000d",
  "160514110637d4800f000005,160514110639d4800f00000a,160514110639d4800f00000e",
  "160514110637d4800f000005,160514110639d4800f00000a,160514110639d4800f00000f",
  "160514110637d4800f000005,160514110639d4800f00000b,160514110639d4800f00000d",
  "160514110637d4800f000005,160514110639d4800f00000b,160514110639d4800f00000e",
  "160514110637d4800f000005,160514110639d4800f00000b,160514110639d4800f00000f"
]

 

二维数组 组合方案

标签:

原文地址:http://www.cnblogs.com/OleRookie/p/5498330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!