<script>
var data = [
{ name1: 'xue', age1: '20' },
{ name2: 'bing', age2: '21' },
{ name3: 'hu', age3: '22' },
{ name4: 'wang', age4: '23' },
{ name5: 'li', age5: '24' },
{ name6: 'wan', age6: '25' },
{ name7: 'kang', age7: '25' },
{ name8: 'hua', age8: '26' },
{ name9: 'kai', age9: '27' },
{ name10: 'fang', age10: '28' },
]
var result = [];
var results = [];
for (var i = 0; i < data.length; i += 3) {
result.push(data.slice(i, i + 3));
}
console.log(result, '分组后1');
if (result.length > 0) {
for (var j = 0; j < result.length; j++) {
var o1 = result[j][0];
var o2 = result[j][1];
var o3 = result[j][2];
var obj = Object.assign(o1, o2, o3);
results.push(obj);
}
}
console.log(data, '分组前2');
console.log(results, '最后3');
</script>
例子本来是10组数据,每三个分一组合并,最后分成四组,主要是数据结构后端不能改,没办法只能前端变;