javascript - 如何使用 jquery 递归地附加此树数据?

标签 javascript jquery arrays json recursion

[{"tag":35,"value":"W","children":[
    {"tag":55,"value":"GOOG","children":null},
    {"tag":262,"value":"ghost332002m0","children":null},
    {"tag":268,"value":"1","children":[
       {"tag":269,"value":"B","children":null},
       {"tag":271,"value":"0","children":null},
       {"tag":336,"value":"3","children":null}
     ]}
  ]},
   {"tag":35,"value":"W","children":[
       {"tag":55,"value":"GOOG","children":null},
       {"tag":262,"value":"ghost332002m0","children":null},
       {"tag":268,"value":"0","children":null}
   ]} 
]

这是 JSON,它是 FIX 市场数据,并且您有这些嵌套组,因此这是我在 JSON 中对这些 FIX 消息的表示。无论如何,我将其发送到我的网络客户端,并需要将其展平回到显示器中。

$.getJSON('/receive', function(data, returnValue) {
    $.each(data, function(index,value) {
        $('#output').append('<p>');
        appendStuff(value);
        $('#output').append('</p>');
    });
function appendStuff(children) {
    debug_var.push(children);
    $.each(children, function(child) {
        $('#output').append(child.tag+'='+child.value+' ');
        if (child.children != null) {
            appendStuff(child.children);
        }
    })
}

我正在尝试使用递归来旋转这些数据并打印出所有内容。我得到的是:

undefined=undefined undefined=undefined undefined=undefined

我做错了什么?

哦,数据都在debug_var中...

enter image description here

最佳答案

您可能想要更接近的东西

$.getJSON('/receive', function (data) {
    $.each(data, function(index,value) {
        $('#output').append('<p>');
        appendStuff(value.children || []);
        $('#output').append('</p>');
    });
    function appendStuff(children) {
        $.each(children, function(i, child) {
            $('#output').append(child.tag+'='+child.value+' ');
            if (child.children != null) {
                appendStuff(child.children);
            }
        });
    }
});

请注意,上面没有打印 parent 的标签,您可能需要添加它。

关于javascript - 如何使用 jquery 递归地附加此树数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377283/

相关文章:

javascript - 使用正则表达式捕获演讲者姓名和评论

javascript - 淡出/淡入和卷起 jquery 效果

javascript - 删除函数中的子 DOM 元素

javascript - 如何在 tinymce 中添加多种语言(印地语和英语)

javascript - 按位置访问文档元素时检测数组

Javascript - 数组连接到另一个

javascript - 如何使用 AJAX 加载 SVG 文件内容?

php - jquery永久改变html

c - 在 C 中将动态数组设置为彼此相等

arrays - 是否可以创建一个结构实例数组?