JavaScritp 中变量的赋值和引用(续),在所有的children中查找某一id的节点并返回

假设每个节点有 id name children 三个属性

function find_node(data,id_find)
		{			
			var toDo = [];			
			toDo.push(data[0]);

			while(toDo.length)
			{
				var node = toDo.shift();
				if(node.id == id_find) return node;
				if (node.children)
				{
					for(var i=0; i<node.children.length; i++)
					{
						var n = node.children[i];
						toDo.push(n);
					}
				}
			}
			return undefined;
		}

发布了36 篇原创文章 · 获赞 33 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/turui/article/details/78377193