test=() => {
const arr = [
{
cycleId: 1,
cycleName: '循环1',
parentCycleId: 0,
type: 'cycle',
versionId: 167,
versionName: '0.1.0',
versionStatusName: '规划中',
}, {
cycleId: 1,
cycleName: '循环1',
parentCycleId: 0,
type: 'cycle',
versionId: 167,
versionName: '0.1.0',
versionStatusName: '规划中',
}, {
cycleId: 1,
cycleName: '循环1',
parentCycleId: 0,
type: 'cycle',
versionId: 167,
versionName: '0.1.0',
versionStatusName: '规划中',
}, {
cycleId: 1,
cycleName: '循环1',
parentCycleId: 0,
type: 'cycle',
versionId: 167,
versionName: '0.1.0',
versionStatusName: '规划中',
},
];
const result = this.sss(arr, 'versionStatusName');
window.console.log(result);
result.forEach((s) => {
s.children = this.sss(s.children, 'versionName');
});
// result = result.map(s => this.sss(s.children, 'versionName'));
window.console.log(result);
}
sss=(arr, key) => {
const result = arr.reduce((groups, item) => {
const groupFound =
groups.find(arrItem => item.title === arrItem[key]);
if (groupFound) {
groupFound.children.push(item);
} else {
// 不要直接在原来的对象修改,新建对象
const newGroup = {
title: item[key],
children: [item],
};
groups.push(newGroup);
}
return groups;
}, []);
return result;
}
console