Guest User

Untitled

a guest
Mar 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. let flatArray = [];
  12. let obj = {
  13. name: 'a',
  14. parent: {
  15. father: {
  16. name: 'af',
  17. parent: {
  18. father: {
  19. name: 'aff'
  20. },
  21. mother: {
  22. name: 'afm'
  23. }
  24. }
  25. },
  26. mother: {
  27. name: 'am',
  28. parent: {
  29. father: {
  30. name: 'amf'
  31. },
  32. mother: {
  33. name: 'amm'
  34. }
  35. }
  36. }
  37. }
  38. }
  39.  
  40.  
  41.  
  42. function getPerson(str, person){
  43.  
  44. let x = {...person};
  45. console.log('=======', str, person)
  46. str += " "+person.name;
  47.  
  48. if( !person.parent ){
  49. flatArray.push(str);
  50. return;
  51. }
  52.  
  53. getPerson(str, person.parent.father);
  54.  
  55. getPerson(str, person.parent.mother);
  56.  
  57. return;
  58.  
  59. }
  60.  
  61. getPerson("", obj);
  62.  
  63. console.log(' flat array is ', flatArray);
  64. </script>
  65.  
  66.  
  67.  
  68. <script id="jsbin-source-javascript" type="text/javascript">let flatArray = [];
  69. let obj = {
  70. name: 'a',
  71. parent: {
  72. father: {
  73. name: 'af',
  74. parent: {
  75. father: {
  76. name: 'aff'
  77. },
  78. mother: {
  79. name: 'afm'
  80. }
  81. }
  82. },
  83. mother: {
  84. name: 'am',
  85. parent: {
  86. father: {
  87. name: 'amf'
  88. },
  89. mother: {
  90. name: 'amm'
  91. }
  92. }
  93. }
  94. }
  95. }
  96.  
  97.  
  98.  
  99. function getPerson(str, person){
  100.  
  101. let x = {...person};
  102. console.log('=======', str, person)
  103. str += " "+person.name;
  104.  
  105. if( !person.parent ){
  106. flatArray.push(str);
  107. return;
  108. }
  109.  
  110. getPerson(str, person.parent.father);
  111.  
  112. getPerson(str, person.parent.mother);
  113.  
  114. return;
  115.  
  116. }
  117.  
  118. getPerson("", obj);
  119.  
  120. console.log(' flat array is ', flatArray);
  121.  
  122. </script></body>
  123. </html>
Add Comment
Please, Sign In to add comment