Home > front end >  The arr of two dimensional array of data processing into newArr the how to deal with? [face] qq: 83.
The arr of two dimensional array of data processing into newArr the how to deal with? [face] qq: 83.

Time:10-30

The arr of two dimensional array of data processing into newArr the how to deal with?
Var arr=[1, 2, 3, 4, 5, 6, 7, 8]
Var newArr=[[1, 2], [3, 4], [5, 6], [7, 8]]

CodePudding user response:

 
Var arr=[1, 2, 3, 4, 5, 6, 7, 8, 9]
Var newArr=[]
While (arr. Length) {
Var tempArr=[]
NewArr. Push (tempArr)
TempArr. Push (arr. Shift ())
If (arr. Length) tempArr. Push (arr. Shift ())
}
The console. The log (JSON. Stringify (newArr))

CodePudding user response:

 
Var arr=[1, 2, 3, 4, 5, 6, 7, 8]
Var newArr=[];
for (var i=0; i NewArr. Push (arr. Slice (I, I + 2));
}
Alert (JSON. Stringify (newArr));

  • Related