Array bidimensional (5X5) .html

<! -
Descripción: Bucle una matriz bidimensional de 5 * 5, a asigna un número natural de 1 a 25.
Salida del medio triángulo inferior izquierdo de la matriz-
>
<script type = "text / javascript">
var bigArr = []
var smallArr = []

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

smallArr.push (i)
//smallArr.length=5 Coloque smallArr en bigArr y deje smallArr vacío

if (smallArr.length === 5) {
bigArr.push (smallArr)
smallArr = []
}
}
console.log (bigArr) / *
(5) [Array (5), Array (5), Array (5), Matriz (5), Matriz (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]
longitud: 5
* /

for (var j = 0; j <bigArr.length; j ++) {
// j === 0 ~ 4
console.log (bigArr [j]) // Salida de matriz pequeña

// 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 ), Matriz (2), matriz (3), matriz (4), matriz (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]
longitud: 5
* /

// Convierta la matriz multidimensional en una matriz de cinco dimensiones
// Método 1:
// matriz de enlaces 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>

Supongo que te gusta

Origin www.cnblogs.com/d534/p/12748258.html
Recomendado
Clasificación