getDependentList

by Rajesh Dixit

HTML

<input type="text" id="txtInput">
<button onclick="getRemoveList()">get Lists</button>

JavaScript

var data = [{
  "_id": "1",
  "parentId": "thisPostId",
  "topLevelId": "1",
  "text": "<p>comment</p>",
}, {
  "_id": "2",
  "parentId": "1",
  "topLevelId": "1",
  "text": "<p>reply to comment</p>",
}, {
  "_id": "3",
  "parentId": "2",
  "topLevelId": "1",
  "text": "<p>reply to reply to comment</p>",
}, {
  "_id": "4",
  "parentId": "3",
  "topLevelId": "1",
  "text": "<p>reply to reply to reply to comment</p>",
}];

function getDependentList(id){
	var retList = [];
  
  data.forEach(function(item){
  	if(item.parentId == id)
    	retList.push(item["_id"]);
  });
  
  if(retList.length > 0){
  	retList.forEach(function(item){
    	retList = retList.concat(getDependentList(item).slice(0));
    });
  }
  
  return retList;
}

function getRemoveList(){
	var id = document.getElementById("txtInput").value;
	var removeList = [];
  removeList.push(id);
  removeList = removeList.concat(getDependentList(id))
	console.log(removeList);
}