• 数组二维(5X5).html


    <!--
    描述:循环一个5*5的二维数组,a赋1~25的自然数
    输出该数组的左下半三角
    -->
    <script type="text/javascript">
    var bigArr=[]
    var smallArr=[]

    for (var i=1;i<=25;i++) {
    //i==1~25

    smallArr.push(i)
    //smallArr.length=5 把 smallArr放到 bigArr里面 在让smallArr为空

    if(smallArr.length===5){
    bigArr.push(smallArr)
    smallArr=[]
    }
    }
    console.log(bigArr) /*
    (5) [Array(5), Array(5), Array(5), Array(5), Array(5)]
    0: (5) [1, 2, 3, 4, 5]
    1: (5) [6, 7, 8, 9, 10]
    2: (5) [11, 12, 13, 14, 15]
    3: (5) [16, 17, 18, 19, 20]
    4: (5) [21, 22, 23, 24, 25]
    length: 5
    */

    for (var j=0;j<bigArr.length;j++) {
    //j=== 0~4
    console.log(bigArr[j]) //输出小数组

    //j===0 bigArr[j].length ===1
    //j===1 bigArr[j].length ===2
    //j===2 bigArr[j].length ===3
    //j===3 bigArr[j].length ===4
    //j===4 bigArr[j].length ===5

    bigArr[j].length=j+1
    }
    console.log(bigArr) /*
    (5) [Array(1), Array(2), Array(3), Array(4), Array(5)]
    0: [1]
    1: (2) [6, 7]
    2: (3) [11, 12, 13]
    3: (4) [16, 17, 18, 19]
    4: (5) [21, 22, 23, 24, 25]
    length: 5
    */

    //把多维数组变成 一个五维数组
    //方法1:
    //concat 链接数组
    var newArr=[]

    for (var k=0;k<bigArr.length;k++) {
    newArr=newArr.concat(bigArr[k])
    }
    console.log(newArr) //(15) [1, 6, 7, 11, 12, 13, 16, 17, 18, 19, 21, 22, 23, 24, 25]

    // var res=newArr.concat(bigArr[0])
    // //console.log(res) //[1]
    // newArr=res
    // //console.log(newArr) //[1]
    // res=newArr.concat(bigArr[1])
    // //console.log(res) //(3) [1, 6, 7]
    // newArr=res
    // //console.log(res) //(3) [1, 6, 7]
    // res=newArr.concat(bigArr[2])
    // console.log(res) //(6) [1, 6, 7, 11, 12, 13]
    // newArr=res
    // res=newArr.concat(bigArr[3])
    // console.log(res) //(10) [1, 6, 7, 11, 12, 13, 16, 17, 18, 19]
    // newArr=res
    // res=newArr.concat(bigArr[4])
    // console.log(res) //(15) [1, 6, 7, 11, 12, 13, 16, 17, 18, 19, 21, 22, 23, 24, 25]



    //方法2:
    //使用toString()方法
    console.log(bigArr.toString().split(',')) //(15) ["1", "6", "7", "11", "12", "13", "16", "17", "18", "19", "21", "22", "23", "24", "25"]


    </script>

  • 相关阅读:
    122.CSS书写顺序及其好处
    120.节流和防抖,Vue中如何添加节流和防抖
    119.css如何使图片固定为正方形
    112.前端css优先级
    vscode设置格式化单引号
    113.CSS怪象记录
    116.vue : 无法加载文件 C:UsersAdministratorAppDataRoaming pmvue.ps1,因为在此系统上禁止运行脚本。
    距离不是一个连续的物理量(Distance is not a continuous physical quantity)
    南七学校信息
    RedisTemplate 序列化问题
  • 原文地址:https://www.cnblogs.com/d534/p/12748258.html
Copyright © 2020-2023  润新知