PIPIONE
如果只有两级的话,用下面这个const result = arr
.filter((a) => a.level === '1')
.map(a => ({
...a, children: arr
.filter(_a => _a.level === '2' && _a.parent_category === a.parent)
}))如果有多级的话,用递归function classify(arr, level = '1', parent_category = '') { const children = arr.filter( a => a.level === `${level}` && a.parent_category === parent_category,
) return children.length === 0
? children
: children.map(a => ({
...a, children: classify(arr, parseInt(a.level, 10) + 1, a.parent),
}))
}const result = classify(arr)