啥也不说了,上代码吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "utf-8" > < script type = "text/javascript" > var num = 4;//每个子数组里的元素个数 var arr = [1,4,5,6,34,34,67,895,2456,87,9,5,23,884,56]; var Arr = new Array(Math.ceil(arr.length/num)); for(var i = 0; i< Arr.length ;i++){ Arr[i] = new Array(); for(var j = 0 ; j<num; j++){ Arr[i][j] = ''; } } for(var i = 0 ; i<arr.length;i++){ Arr[parseInt(i/num)][i%num] = arr[i]; } console.log(Arr) </script> </ head > < body > </ body > </ html > |
以上这篇JavaScript实现一维数组转化为二维数组就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。