Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. var obj = [{
  2. id: 1,
  3. name: 'abc',
  4. child: [{
  5. id: 2,
  6. name: 'xyz',
  7. child: [{
  8. id: 3,
  9. name: 'abcd',
  10. child: []
  11. }, {
  12. id: 3,
  13. name: 'abcd',
  14. child: []
  15. }]
  16. }]
  17. }, {
  18. id: 100,
  19. name: 'dfsdfdsf',
  20. child: [{
  21. id: 101,
  22. name: 'xsdfsfyz',
  23. child: [{
  24. id: 102,
  25. name: 'sdfsfsdfsd',
  26. child: []
  27. }, {
  28. id: 103,
  29. name: 'sdfsdfsfd',
  30. child: []
  31. }]
  32. }]
  33. }];
  34.  
  35.  
  36. function recursion(input, arr = []) {
  37. if (!input.length) {
  38. return arr;
  39. }
  40. input.forEach(e => {
  41. arr.push({
  42. id: e.id,
  43. name: e.name
  44. });
  45. recursion(e.child, arr);
  46. });
  47. return arr;
  48. }
  49. console.log(recursion(obj, []));
Add Comment
Please, Sign In to add comment