Guest User

Untitled

a guest
Sep 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // Build a deep object of some sort
  2. var ob = {
  3. type: 'windar',
  4. title: 'Hello World',
  5. children: [
  6. {
  7. type: 'BUTTUN',
  8. title: 'click me',
  9. },
  10. {
  11. type: 'view',
  12. title: 'I have no children',
  13. children: [ ]
  14. },
  15. {
  16. type: 'view',
  17. title: 'I have 3 children',
  18. children: [
  19. {
  20. type: 'view',
  21. title: 'I also have a child',
  22. children: [
  23. {
  24. type: 'label',
  25. title: 'Don\'t label me!'
  26. }
  27. ]
  28. },
  29. {
  30. type: 'PICTURE',
  31. title: 'look at me',
  32. },
  33. {
  34. type: 'INPUT',
  35. title: 'type in me',
  36. }
  37. ]
  38. }
  39. ]
  40. };
  41.  
  42. // Recursively loops an object and it's children and modifies them
  43. // Works it's way BACKWARDS through the children - so the array counter doesn't put us in an odd place
  44. // Destroys the DEEPEST objects first - so we don't lose referece
  45. var counter = 0;
  46. var recursive_killing = function(ob, parent){
  47. // Do we have children?
  48. if(ob.children){
  49. for (var c=(ob.children.length-1); c>=0; c--){
  50. recursive_killing(ob.children[c], ob);
  51. }
  52. }
  53. // If we have a parent (ie not the root node)
  54. if(parent){
  55. // Do whatever you need to kill the object here
  56. // I'm just setting the title to DEAD and showing the order we killed it with a counter)
  57. counter ++;
  58. ob.title = 'DEAD (' + counter + ')';
  59. }
  60. }
  61.  
  62. // Run the function
  63. recursive_killing(ob);
Add Comment
Please, Sign In to add comment