function getListLeafNode (list, prop, tmpList = []) {
for (let i = 0; i < list.length; i++) {
if (list[i][prop] && list[i][prop].length) {
getListLeafNode(list[i][prop], prop, tmpList)
} else {
tmpList.push(list[i])
}
}
return tmpList
}
样例
let dataList = [{
id: '1',
children: [
{id: '11'},
{id: '12'}
]
}, {
id: '2'
}, {
id: '3'
}
]
console.log(getListLeafNode(dataList, 'children'))
运行结果
[
{id: '11'},
{id: '12'},
{id: '2'},
{id: '3'}
]