Javascript 如何使用jquery递归地附加此树数据?
这里有JSON,它是修复市场数据,您有这些嵌套的组,所以这是我用JSON表示的修复消息。无论如何,我将它发送到我的web客户端,并需要在显示器中将其展平Javascript 如何使用jquery递归地附加此树数据?,javascript,jquery,arrays,json,recursion,Javascript,Jquery,Arrays,Json,Recursion,这里有JSON,它是修复市场数据,您有这些嵌套的组,所以这是我用JSON表示的修复消息。无论如何,我将它发送到我的web客户端,并需要在显示器中将其展平 [{"tag":35,"value":"W","children":[ {"tag":55,"value":"GOOG","children":null}, {"tag":262,"value":"ghost332002m0","children":null}, {"tag":268,"value":"1","child
[{"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}
]}
]
我做错了什么
哦,数据都在调试变量中
你可能想要更接近的东西
undefined=undefined undefined=undefined undefined=undefined
$.getJSON('/receive',函数(数据){
$.each(数据、函数(索引、值){
$(“#输出”)。追加(“”);
appendStuff(value.children | |[]);
$(“#输出”)。追加(“”);
});
函数(子函数){
$。每个(子项,函数(i,子项){
$('#output').append(child.tag+'='+child.value+'');
if(child.children!=null){
附属物(child.children);
}
});
}
});
注意:上面没有打印父母的标签,您可能需要添加这些标签。您可能需要更接近父母的标签
undefined=undefined undefined=undefined undefined=undefined
$.getJSON('/receive',函数(数据){
$.each(数据、函数(索引、值){
$(“#输出”)。追加(“”);
appendStuff(value.children | |[]);
$(“#输出”)。追加(“”);
});
函数(子函数){
$。每个(子项,函数(i,子项){
$('#output').append(child.tag+'='+child.value+'');
if(child.children!=null){
附属物(child.children);
}
});
}
});
请注意,上面没有打印家长的标签,您可能需要添加这些标签。JavaScript的工作方式与HTML不同。您误用了
append
方法。你能发布预期的标记吗?我不理解你的评论$(“#输出”).append(变量);将变量的内容注入到我的一件事中,我注意到appendStuff将children数组作为参数,但当它使用value进行调用时。也是以$为单位。回调的第一个参数是数值索引,而不是对象本身。JavaScript与HTML不一样。您误用了append
方法。你能发布预期的标记吗?我不理解你的评论$(“#输出”).append(变量);将变量的内容注入到我的函数中,我注意到的一件事是appendStuff将子数组作为参数,但当它用value来调用时。同样是在$中。回调的第一个参数是数值索引,而不是对象本身。
$.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);
}
});
}
});