<!--
描述:循环一个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>