当我在Firefox上运行应用程序时,我在下面的一段代码中得到了太多的递归错误。该代码有望在浏览器中创建图形树结构。该方法将Json作为输入,遍历键并检查对应于该键的值是否为“Leaf”。如果值为“Leaf”,则将其显示在浏览器上并移至下一个键,否则它将递归遍历子节点,直到达到“Leaf”。所有子节点都显示在浏览器上:
function createmasterJson(data) {
var json = '';
for (var index in data) {
var child = data[index];
if (child == 'Leaf') {
json = json + createLeafNode(index);
} else {
if (child.length) {
for (var i = 0; i < child.length; i++) {
json = json + createNonLeafNode(index, createmasterJson(child[i]));
}
} else {
json = json + createLeafNode(index);
}
}
}
return json;
}
function createLeafNode(index){
return '{ "attributes": { "id" :"' + index + '_' + Math.random() +'"}, "data": " '+ index +'" },';
}
function createNonLeafNode(index, child){
return '{ "attributes": { "id" :"' + index + '_' + Math.random() +'"}, "data": " '+ index +'" ,"state": "closed", "children" : ['+child.substring(0, child.length - 1)+']},';
}