JAVASCRIPT一维数转化为二维数组

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <script type="text/javascript">
        var num = 4;//每个子数组里的元素个数
        var arr = [1,4,5,6,34,34,67,895,2456,87,9,5,23,884,56];
        var Arr = new Array(Math.ceil(arr.length/num));
        for(var i = 0; i<Arr.length;i++){
        Arr[i] = new Array();
        for(var j = 0; j<num; j++){
        Arr[i][j] = '';
        }
        }
        for(var i = 0; i<arr.length;i++){
        Arr[parseInt(i/num)][i%num] = arr[i]; 
        } 
        console.log(Arr)
        </script>
    </head>
    <body>
    </body>
</html>

  可以仔细研究一下

posted @ 2018-05-22 14:50  黑发不知勤学早  阅读(216)  评论(0编辑  收藏  举报