日期:2014-05-16 浏览次数:20500 次
var arr=[1,2,3,4,5,6,7,8,9,10];
如何转换成
[[1,2,3][4,5,6][7,8,9][10]]
var arr=[1,2,3,4,5,6,7,8,9,10];
function splitArray(aim , size)
{
var result = [];
var tempArray;
for(var count = 0; count < aim.length; count++)
{
if(count%size == 0 )
{
if(tempArray)
{
result.push(tempArray);
}
tempArray = [];
}
tempArray[tempArray.length]=aim[count];
if(count+1 == aim.length)
{
result.push(tempArray);
}
}
return result;
}
alert(splitArray(arr,3)[0]);
alert(splitArray(arr,3)[1]);
alert(splitArray(arr,3)[2]);
alert(splitArray(arr,3)[3]);
var arr=[1,2,3,4,5,6,7,8,9,10];
console.log(JSON.stringify(arr));
var temp = [];
for(var i=0;i<arr.length;++i){
temp = arr.slice(i,i+3);
arr.splice(i,3,temp);
}
console.log(JSON.stringify(arr));
function array_change(a,size){//size为二维数组中的每一维的元素个数
var cnt=Math.ceil(a.length/size);
//alert(cnt);
var br=new Array();
for(var i=0;i<cnt;i++){
br[i]=new Array();
br[i] = a.slice(size*i,size*i+parseInt(size));//br[i] = a.slice(0,3);//br[i] = a.slice(3,6);//br[i] = a.slice(6,9);
alert(br[i]);
}
return br;
}
array_change(a,3);
var a = [1,2,3,4,5,6,7,8,9,10],arr=t=[];
while((t = a.splice(0,3)).length){
arr[arr.length] = t;
}