row行*column列数据,按t间隔均匀分配,返回数组下标

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/songhuageini/article/details/82697015
<script>
        var rowNum = 6; //行数
        var columnNum = 7; //列数
        var t = 2; //间隔

        /*每行都计入*/
        function getGeDianArrRowNoChange() {
            var arr = [];
            var cFor = parseInt(columnNum / t);
            if(t == 1){
                cFor = cFor - 1;
            }
            for(var r = 0; r < rowNum; r++) {
                for(var c = 0; c <= cFor; c++) {
                    var res = r * columnNum + t * c;
                    if(res >= rowNum * columnNum) {
                        break;
                    }
                    arr.push(res);
                }
            }
            return arr;
        }
        var res = getGeDianArrRowNoChange();
        console.log(res);//(24) [0, 2, 4, 6, 7, 9, 11, 13, 14, 16, 18, 20, 21, 23, 25, 27, 28, 30, 32, 34, 35, 37, 39, 41]

        /*行列均匀分配*/
        function getGeDianArr() {
            var arr = [];
            var cFor = parseInt(columnNum / t);
            if(t == 1){
                cFor = cFor - 1;
            }
            for(var r = 0; r < rowNum; r = r + t) {
                for(var c = 0; c <= cFor; c++) {
                    var res = r * columnNum + t * c;
                    if(res >= rowNum * columnNum) {
                        break;
                    }
                    arr.push(res);
                }
            }
            return arr;
        }
        var res = getGeDianArr();
        console.log(res);//(12) [0, 2, 4, 6, 14, 16, 18, 20, 28, 30, 32, 34]

        /*行列均匀分配*/
        function getGeDianArr1() {
            var arr = [];
            for(var r = 0; r < rowNum; r += t) {
                for(var c = 0; c < columnNum; c += t) {
                    var res = columnNum * r + c;
                    if(res >= rowNum * columnNum) {
                        break;
                    }
                    arr.push(res);
                }
            }
            return arr;
        }
        var res = getGeDianArr1();
        console.log(res);//(12) [0, 2, 4, 6, 14, 16, 18, 20, 28, 30, 32, 34]

    </script>
展开阅读全文

没有更多推荐了,返回首页