function buildTree(arr, parentId = null) { let tree = []; for (let i = 0; i < arr.length; i++) { let item = arr[i]; if (item.parentId === parentId) { let children = buildTree(arr, item.id); if (children.length) { item.children = children; } tree.push(item); } } return tree; }
标签:arr,递归函数,tree,item,树形,let,parentId,多维,children From: https://www.cnblogs.com/chenlw/p/17260362.html