var input = {"A":["B","C"],"B":["D","C","F"],"C":["E"],"D":[],"E":["D"],"F":["E","C"]}; var arr = new Array(); while(JSON.stringify(input) !="{}"){ var res; for(var key1 in input){ if(input[key1].length == 0){ res = key1; console.log(res); delete input[key1]; break; } } for(var key2 in input){ var a = input[key2]; for(var k = 0; k < a.length; k++){ if(a[k] == res){ var del = a.splice(k,1); } } } } console.log(arr);