爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB

  1. "use strict";
  2. function handleTree(data, id, parentId, children) {
  3. let config = {
  4. id: id || "id",
  5. parentId: parentId || "parentId",
  6. childrenList: children || "children"
  7. };
  8. var childrenListMap = {};
  9. var nodeIds = {};
  10. var tree = [];
  11. for (let d of data) {
  12. let parentId2 = d[config.parentId];
  13. if (childrenListMap[parentId2] == null) {
  14. childrenListMap[parentId2] = [];
  15. }
  16. nodeIds[d[config.id]] = d;
  17. childrenListMap[parentId2].push(d);
  18. }
  19. for (let d of data) {
  20. let parentId2 = d[config.parentId];
  21. if (nodeIds[parentId2] == null) {
  22. tree.push(d);
  23. }
  24. }
  25. for (let t of tree) {
  26. adaptToChildrenList(t);
  27. }
  28. function adaptToChildrenList(o) {
  29. if (childrenListMap[o[config.id]] !== null) {
  30. o[config.childrenList] = childrenListMap[o[config.id]];
  31. }
  32. if (o[config.childrenList]) {
  33. for (let c of o[config.childrenList]) {
  34. adaptToChildrenList(c);
  35. }
  36. }
  37. }
  38. return tree;
  39. }
  40. exports.handleTree = handleTree;
  41. //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/tree.js.map