JS实现二维数组元素的排列组合运算简单示例
阿来小同学 人气:0本文实例讲述了JS实现二维数组元素的排列组合运算。分享给大家供大家参考,具体如下:
用js实现二维数组里面的元素排列组合一个小demo;
源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS数组排列组合运算</title> </head> </head> <body> <textarea id="value" style="width:600px; height:500px;"></textarea> </body> <script type="text/javascript"> var arrays = [["a0","a1","a2","a3"],["b0","b1","b2","b3"],["c0","c1","c2","c3"],["d0","d1","d2","d3"]]; var array = getArrayByArrays(arrays); document.getElementById('value').value = array+"....."+array.length; function getArrayByArrays(arrays) { var arr = [""]; for(var i = 0;i<arrays.length;i++) { arr = getValuesByArray(arr,arrays[i]); } return arr; } function getValuesByArray(arr1,arr2) { var arr = []; for(var i=0;i<arr1.length;i++) { var v1 = arr1[i]; for(var j=0;j<arr2.length;j++) { var v2 = arr2[j]; var value = v1+v2; arr.push(value); }; }; return arr; } </script> </html>
运行结果:
希望本文所述对大家JavaScript程序设计有所帮助。
加载全部内容