JavaScript 二维数组排列组合2

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

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

	<script type="text/javascript">
		var arrays = [ 
			[ 
				'1-1-雨尘', '1-2-芸芸', '1-3-简一', '1-4-乐乐'
			]
			, [ 
				'2-5-小明', '2-6-花花', '2-7-数数'
			]
			, [ 
				'3-8-静静', '3-9-点点', '3-10-hapday', '3-11-欢欢', '3-12-yuchen'
			] 
		];

		var array = getArrayByArrays(arrays);

		window.document.getElementById('showDiv').innerHTML = '共计 ' + array.length + ' 种组合。<br /><br />';

		for (var index = 0; index < array.length; index++) {
			var row = array[index];
			var rows = row.split('#');

			for (var cursor = 0; cursor < rows.length; cursor++) {
				var cell = rows[cursor];
				var cells = cell.split('-');

				for (var kk = 0; kk < cells.length; kk++) {
					window.document.getElementById('showDiv').innerHTML += cells[kk] + '    '; 
				}

				window.document.getElementById('showDiv').innerHTML += '                                ';
			}

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

			/**
			 * 遍历外层数组
			*/
			for (var index = 0; index < arrays.length; 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 target = '';
					if (null !== value1 && undefined !== value1 && '' !== value1) {
						target = value1 + '#' + value2;
					} else {
						target = value2;
					}

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

			return arr;
		};

	</script>

</html>

  

posted @ 2019-03-18 00:27  hapday  阅读(533)  评论(0编辑  收藏  举报