1
我試圖創建一個可遍歷JSON樹並更改其數據/結構的函數。我設法讓它遍歷每個節點並讀取數據,但堅持改變它的數據/結構部分。遍歷JSON樹並操作其數據/結構
function jsonTraverser(tree){
$.each(tree, function(index, val){
if(val.tag == 'button'){
delete val[index];
}
if(val.children){
jsonTraverser(val.children);
}
})
}
jsonTraverser(json_obj);
從上面的代碼中,它應該刪除標籤等於按鈕的JSON節點。雖然它似乎有效,但它不會刪除一個節點,而更改它的標記工作得很好。你有解決這個問題的辦法嗎?
PS:如果您需要JSON樹樣本,那麼就是這樣。
[
{"tag":"div","id":"0","class":"sgph","children":[
{"tag":"div","data-scoretype":"","class":"sgi","id":"1","children":[
{"tag":"button","data-btnrole":"satis_group","class":"up","html":"ขึ้น"},
{"tag":"button","data-btnrole":"satis_group","class":"down","html":"ลง"},
{"tag":"button","data-btnrole":"satis_group","class":"edit","html":"เปลี่ยนกลุ่ม"},
{"tag":"button","data-btnrole":"satis_group","class":"del","html":"ลบกลุ่ม"}
]},
{"tag":"div","class":"scph","children":[
{"tag":"button","data-btnrole":"score_type","class":"add","html":"เกณฑ์การให้คะแนน"}
]},
{"tag":"div","class":"stph","children":[
{"tag":"div","class":"sti","id":"3","children":[
{"tag":"button","data-btnrole":"satis_topics","class":"up","html":"ขึ้น"},
{"tag":"button","data-btnrole":"satis_topics","class":"down","html":"ลง"},
{"tag":"button","data-btnrole":"satis_topics","class":"edit","html":"เปลี่ยนหัวข้อ"},
{"tag":"button","data-btnrole":"satis_topics","class":"del","html":"ลบหัวข้อ"}
]},
{"tag":"button","data-btnrole":"satis_topics","class":"add","html":"เพิ่มหัวข้อ"}
]}
]},
{"tag":"div","id":"1","class":"sgph","children":[
{"tag":"button","data-btnrole":"satis_group","class":"add","html":"เพิ่มกลุ่ม"}
]}
]
v.children or val.children? – 2014-10-29 08:29:03
val [i]沒有任何意義,val包含名爲「tag」的屬性, – dandavis 2014-10-29 08:29:19