有时后端常常返回的是树状结构,我们需要进行平铺操作,而且只获取没有孩子的节点(即最深层的节点)。
flatList(menuList) {
let list = [];
if (menuList.length > 0) {
for (let index = 0, length = menuList.length; index < length; index++) {
const node = menuList[index];
if (node.children.length == 0) {
list.push(node);
}
list = list.concat(this.flatList(node.children));
}
return list;
} else {
return [];
}
}