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; iNewArr. Push (arr. Slice (I, I + 2));
}
Alert (JSON. Stringify (newArr));