2015-05-02 34 views
0
[ 
    { 
     task : { id : 1, id_parent : "",  , is_parent : true  } 
    }, 
    { 
     task : { id : 2, id_parent : "",  , is_parent : true  } 
    }, 
    { 
     task : { id : 3, id_parent : "1", , is_parent : false  } 
    }, 
    { 
     task : { id : 4, id_parent : "2", , is_parent : false  } 
    }, 
    { 
     task : { id : 5, id_parent : "1", , is_parent : false  } 
    }, 
    { 
     task : { id : 6, id_parent : "1", , is_parent : true  } 
    }, 
    { 
     task : { id : 7, id_parent : "2", , is_parent : false  } 
    }, 
    { 
     task : { id : 8, id_parent : "2", , is_parent : true  } 
    }, 
    { 
     task : { id : 9, id_parent : "1", , is_parent : true  } 
    }, 
    { 
     task : { id : 10, id_parent : "9", , is_parent : false  } 
    }, 
    { 
     task : { id : 11, id_parent : "9", , is_parent : false  } 
    }, 
    { 
     task : { id : 12, id_parent : "8", , is_parent : true  } 
    }, 
    { 
     task : { id : 13, id_parent : "2", , is_parent : false  } 
    }, 
    { 
     task : { id : 14, id_parent : "6", , is_parent : true  } 
    }, 
    { 
     task : { id : 15, id_parent : "12", , is_parent : true  } 
    }, 
    { 
     task : { id : 16, id_parent : "15",  , is_parent : false  } 
    }, 
    { 
     task : { id : 17, id_parent : "8", , is_parent : false  } 
    }, 
    { 
     task : { id : 18, id_parent : "15",  , is_parent : false  } 
    } 
]  
// return direct child of the id_task 
    // where this direct child is a prent 
    this.getDirectChildParent = function(id_task, array_tasks){ 
     var tasks = array_tasks; 
     if (array_tasks === undefined) { 
      tasks = this.getAllTasks(); 
     } 
     var allChildParent = []; 
     for (var i = 0; i < tasks.length; i++) { 
      var task = tasks[i].task; 
      if(task.is_parent && task.id_parent == id_task) allChildParent.push(task.id); 
     } 
     return allChildParent; 
    } 

    // return direct child of the id_task 
    // where this direct child is not a prent 
    this.getDirectNotParentChild = function(id_task, array_tasks){ 
     var tasks = array_tasks; 
     if (array_tasks === undefined) { 
      tasks = this.getAllTasks(); 
     } 
     var allDirectChild = []; 
     for (var i = 0; i < tasks.length; i++) { 
      var task = tasks[i].task; 
      if(!task.is_parent && task.id_parent == id_task) { 
       allDirectChild.push(task.id); 
       /*tasks.splice(i, 1); 
       i--;*/ 
      } 
     } 
     return allDirectChild; 
    } 
this.getAllChilderenTask = function(id_task, array_tasks){ 
     var id_child = []; 
     var tasks = array_tasks; 

     if (array_tasks === undefined) { 
      tasks = this.getAllTasks(); 
     } 

     id_child = id_child.concat(this.getDirectNotParentChild (id_task, tasks)); 
     id_child_parent = this.getDirectChildParent (id_task, tasks); 
     id_child = id_child.concat(id_child_parent); 

     if(id_child_parent.length != 0){ 
      for (var i = 0; i < id_child_parent.length; i++) { 
       id_child = id_child.concat(this.getAllChilderenTask (id_child_parent[i], tasks)); 
      } 
     } 

     return id_child; 
    } 

您好,
我試圖做的是恢復我的孩子陣列功能。 我的問題是,當我用我的功能,它的工作原理,但它並沒有給我選擇的道路的所有孩子,例如,當我做
object.getAllChilderenTask(1)
它返回:
陣列[3,5,6, 9,14]
但是正確的應該是:[3,5,6,9,10,11,14]。
我發現了這個問題,但我不知道如何解決它。我對()循環沒有完成所有元素JS

if(id_child_parent.length != 0){ 
      for (var i = 0; i < id_child_parent.length; i++) { 
       id_child = id_child.concat(this.getAllChilderenTask (id_child_parent[i], tasks)); 
      } 
     } 

它只爲第一個元素(WTF)循環。
如果我從循環中刪除id_child,它循環所有元素,我使用遞歸方法(getAllChilderenTask)。

你有什麼想法嗎?

+0

你應該讓一個js小提琴來顯示你的輸出和你期望的輸出 – tommybananas

+0

你不需要把你的for循環包裹在一個if來檢查長度。 for循環'i scrappedcola

+0

#scrappedcola,我真的忘記了他們的代碼。不管怎麼說,還是要謝謝你 – user3514903

回答

0

那麼,我的錯誤是,在我的遞歸方法中,我使用了一個變量id_parent_child,就像全局變量一樣,所以當我的程序工作的時候,它調用了這個變化的同樣的方法。 解決的辦法是添加var id_child_parent = [];
是一個本地方法變量。

this.getAllChilderenTask = function(id_task, array_tasks){ 
      var id_child = []; 
      var id_child_parent = []; 
      var tasks = array_tasks; 

      if (array_tasks === undefined) { 
       tasks = this.getAllTasks(); 
      } 

      id_child = id_child.concat(this.getDirectNotParentChild (id_task, tasks)); 
      id_child_parent = this.getDirectChildParent (id_task, tasks); 
      id_child = id_child.concat(id_child_parent); 

      if(id_child_parent.length != 0){ 
       for (var i = 0; i < id_child_parent.length; i++) { 
        id_child = id_child.concat(this.getAllChilderenTask (id_child_parent[i], tasks)); 
       } 
      } 

      return id_child; 
     } 

這就是全部。 :)