Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. const mappedData = this.state.treeData
  2. .filter(data => data.title === "Category")
  3. .map(categ => {
  4. const { id, title, children, subtitle, type } = categ;
  5.  
  6. function getChildren(children) {
  7. return children
  8. ? children.map(child => {
  9. if (child.title === "Item" || child.title === "Group") {
  10. const data = {
  11. id: child.id,
  12. name: child.subtitle,
  13. type: child.type,
  14. children: getChildren(child.children),
  15. child_id: child.children
  16. ? child.children.map(child => child.id)
  17. : []
  18. };
  19. if (data.children.length === 0) delete data.children;
  20. if (data.child_id.length === 0) delete data.child_id;
  21.  
  22. return data;
  23. } else {
  24. return {
  25. id: child.id,
  26. name: child.subtitle,
  27. type: child.type
  28. };
  29. }
  30. })
  31. : [];
  32. }
  33.  
  34. const data = {
  35. id: id,
  36. name: subtitle,
  37. type: type,
  38. children: getChildren(children),
  39. child_id: children ? children.map(child => child.id) : []
  40. };
  41.  
  42. if (data.children.length === 0) delete data.children;
  43. if (data.child_id.length === 0) delete data.child_id;
  44.  
  45. return data;
  46. });
  47.  
  48. <div className="json">
  49. <p> {JSON.stringify(mappedData)}</p>
  50. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement