项目中会遇到为了渲染方便要把后台给的竖向数组排列为横向数组
例:后台传回的数组为[2004, 2005, 2006, 2007, 2008]
[46890000, 55900500, 33786400, 57054400, 61834100]
[28440000, 29340700, 15127600, 16771500, 36568800]
但是页面上要展示成这样的效果:
function changearr(oldarr){ console.log(oldarr); var maxLength=getMaxLength(oldarr); //console.log(maxLength); var rowarr=[],rowoldarr=[]; for(var k=0;k<oldarr.length;k++){ for(var m=0;m<maxLength;m++){ if (!rowarr[m]) rowarr[m]=[]; rowarr[m].push(oldarr[k][m]); if (!rowoldarr[m]) rowoldarr[m]=[]; rowoldarr[m].push(oldarr[k][m]); } } //console.log(rowarr); return rowarr; } function getMaxLength(arr) { //console.log(arr); var max=0; for(var i=0; i<arr.length; i++){ if(arr[i].length>max){ max=arr[i].length; } } return max; }
var oldarr=[
[2004, 2005, 2006, 2007, 2008],
[46890000, 55900500, 33786400, 57054400, 61834100],
[28440000, 29340700, 15127600, 16771500, 36568800]
]
var newarr=changearr(oldarr);
console.log(newarr)
结果如下:
这样页面渲染就方便多了