js,树形数组 遍历获取某个节点,跪求大神
vara=[{"id":"0000","text":"R1","children":[{"id":"8978","text":"Aad","children":[{"id...
var a=[ { "id" : "0000", "text" : "R1", "children" : [ { "id" : "8978", "text" : "Aad", "children" : [ { "id" : "2312", "text" : "adaada", "children" : [ { "id" : "5154", "text" : "asdsa" }] },{ "id" : "4544", "text" : "afasf", "children" : [ { "id" : "5236", "text" : "afasf" }, { "id" : "2328", "text" : "afasf" } ] }] }, { "id" : "7867", "text" : "R2", "children" : [ { "id" : "8767", "text" : "afasf", "children" : [ { "id" : "2016", "text" : "afafa" }, { "id" : "2017", "text" : "afasd" } ] }, { "id" : "7657", "text" : "h", "children" : [ { "id" : "7867", "text" : "afras" } ] } ] } ] } ];
这种树形json数组想实现一个功能
比如我想给个值var id=“2016”;
就把2016这个里的所有东西拿出来
var json={ "id" : "2016", "text" : "afafa" }; 展开
这种树形json数组想实现一个功能
比如我想给个值var id=“2016”;
就把2016这个里的所有东西拿出来
var json={ "id" : "2016", "text" : "afafa" }; 展开
1个回答
展开全部
这就是最基础的遍历
var GetByID = function(Data,ID)
{
var Deep,T,F;
for (F = Data.length;F;)
{
T = Data[--F]
if (ID === T.id) return T
if (T.children)
{
Deep = GetByID(T.children,ID)
if (Deep) return Deep
}
}
}
GetByID(a,'2016')
更多追问追答
追问
厉害啊 之前一直是搞java js不 太熟悉 你这种写法 不太常见 有什么好的学习方法?
追答
欸..
是那里你觉得不常见?
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询