let arr = [{
name: '班长',
role: '01'
},{
name: '队员',
role: '02'
},{
name: '队员',
role: '02'
}].reverse();
var result = {}
var key ='children'
s(arr)
function s(arrs) {
arrs.forEach((o, i) => {
// console.log('---'+ i + ': ', result)
if (!result[key]) {
if(i === 0) return result = o
result[key] = [o]
} else {
o[key] = [result]
result = o
}
})
}
console.log(result)