求解如何循环生成这种json

var c={


         0:[0],

         1:[1,9],

         2:[2,10,18],

         3:[3,11,19,27],

         4:[4,12,20,28,36],

         5:[5,13,21,29,37,45],

         6:[6,14,22,30,38,46,54],

         7:[7,15,23,31,39,47,55,63],

         8:[8,16,24,32,40,48,56,64],

         9:[17,25,33,41,49,57,65],

        10:[26,34,42,50,58,66],

        11:[35,43,51,59,67],

        12:[44,52,60,68],

        12:[53,61,69],

        12:[62,70],

        12:[71],

    }


杨魅力
浏览 377回答 1
1回答

素胚勾勒不出你

function gen(num) {&nbsp; let m = Math.floor(num / 2)&nbsp; let result = {}&nbsp; for (let i = 0; i < m; i++) {&nbsp; &nbsp; result[i] = [...Array(i + 1).keys()].map(j => i + j * m)&nbsp; }&nbsp; let k = num % 2 ? m + m * m : m * m&nbsp; for (let i = m; i < num; i++, k++) {&nbsp; &nbsp; result[i] = [...Array(num - i).keys()].map(j => k - j * m).reverse()&nbsp; }&nbsp;&nbsp;&nbsp; return result}console.table(gen(16))效果截图如下:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript