Home > front end >  Array every two merger
Array every two merger

Time:11-06

The original data every two merged into one and form a new array, for the whole morning and didn't out
The original data:

{name: zhang SAN},
{name: li si},
{name: fifty},
{name: find six}
]
Form new data for:

[{name: zhang SAN}, {name: li si}],
[{name: fifty}, {name: zhao six}]
]

CodePudding user response:

Problem has been solved, scattered points tomorrow

CodePudding user response:

66

CodePudding user response:

Although the original poster is solved, but still try to write the
 

Arr. Reduce ((obj, v, I)=& gt; {
If (I % 2===0) {
Obj [obj. Length]=[v]
} else {
Obj [obj. Length - 1]. Push (v)
}
Return obj
}, [])

CodePudding user response:

 
Var arr=[
{name: "* *"},
{name: "bill"},
{name: "detective"},
{name: "find six"}
];
Var arr2=[];
For (var I=0; I & lt; Arr. Length; I +=2)
Arr2. Push (arr. Slice (I, I + 2));
The console. The log (arr2);
  • Related