树形转数组

  1. /** 属性结构添加路径 */
  2. export const addPathKey = (list: (DemandProps.TreeNodeData & { pathKey?: string })[], parentKey?: string) => {
  3. list.forEach((item) => {
  4. const pathKey = parentKey ? `${parentKey}-${item.value}` : item.value;
  5. item.value = pathKey;
  6. if (Array.isArray(item.children) && item.children.length > 0) {
  7. addPathKey(item.children, pathKey);
  8. }
  9. });
  10. return list;
  11. };
  12. export const addClueKey = (list: any[], parentKey = '') => {
  13. list.flatMap(item => {
  14. const pathKey = parentKey ? `${parentKey}-${item.value}` : item.value;
  15. item.value = pathKey;
  16. if (item.children) {
  17. return addClueKey(item.children, pathKey)
  18. }
  19. return item;
  20. })
  21. return list;
  22. }