Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何递归搜索使用observableArray的树?_Javascript_Jquery_Recursion_Knockout.js - Fatal编程技术网

Javascript 如何递归搜索使用observableArray的树?

Javascript 如何递归搜索使用observableArray的树?,javascript,jquery,recursion,knockout.js,Javascript,Jquery,Recursion,Knockout.js,我有一个侧栏索引菜单,它是一个复杂的嵌套树。这是使用TreeNode数据结构创建的,如下所示: function TreeNode(data){ var self = this; self.key = ko.observable(data.key); self.label = ko.observable(data.label); self.children = ko.observableArray([]); self.documentPropertyCl

我有一个侧栏索引菜单,它是一个复杂的嵌套树。这是使用TreeNode数据结构创建的,如下所示:

function TreeNode(data){
    var self = this;

    self.key = ko.observable(data.key);
    self.label = ko.observable(data.label);
    self.children = ko.observableArray([]);
    self.documentPropertyClassId = ko.observable(data.document_property_class_id);

    // Recursively create new children
    $.each(data.children, function(index, child){
            self.children.push(new TreeNode(child));
    });
    self.isFilterable = ko.pureComputed(function(){
        // If a node has no children, then it's a filterable
        return !ko.utils.unwrapObservable(self.children).length;
    });
}
这是根据Ajax请求创建的:

var response = getData(label, id);

response.then(function(data){
    self.treeNode(new TreeNode(data, self));
});
这会产生这样一个列表,但实际上并非如此(我从其他地方复制了链接)

我正在努力解决的是递归搜索树中包含匹配documentPropertyClassId的节点

这是我在互联网上发现的,我正在尝试适应淘汰赛Js领域:

function searchTree(element, matchingTitle){
    if(element.title == matchingTitle){
        return element;
    }else if (element.children != null){
        var i;
        var result = null;
        for(i=0; result == null && i < element.children.length; i++){
            result = searchTree(element.children[i], matchingTitle);
        }
        return result;
    }
    return null;
}

你走了多远?我可以看出您需要使用
result=searchTree(child,matchingPropertyId)
而不是
child.children
可观察树和简单树之间有什么区别吗?你能创建一个吗?在没有实际上下文/代码的情况下填空有点困难。你做了多少?我可以看出您需要使用
result=searchTree(child,matchingPropertyId)
而不是
child.children
可观察树和简单树之间有什么区别吗?你能创建一个吗?在没有访问实际上下文/代码的情况下填空有点困难。
function searchTree(node, matchingPropertyId){
    if(node.documentPropertyClassId() == matchingPropertyId) {
        console.log("found");
        return node;
    } else if (ko.utils.unwrapObservable(node.children).length > 0) {
       var result = null;
       console.log("hello");
       ko.utils.arrayForEach(node.children(), function(child) {
           console.log(child.label());
           result = searchTree(child.children, matchingPropertyId);
       });
       return result;
    }

    return null;
}