用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></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>
运行结果:
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:测试上述代码运行效果。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数据结构与算法技巧总结》、《JavaScript数组操作技巧总结》、《JavaScript数学运算用法总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》