如何找到 javascript 和 json 组的所有叶子

how to find all leaves of group with javascript and json

我想列出组名称所在的所有叶子 ID,即 "group110" 因此,此示例的输出为 014、288、223 和 244。

这是我的 JSON 文件的内容:

{
 "name": "visualization",
 "children": [
        {
         "name": "group100",
         "children": [
                {
                 "name": "group110",
                 "children": [
                        {
                         "name": "group111",
                         "children": [
                                        {"id":"014","link":"-","name":" Animals/70","decade":"-"}
                                ]
                        },
                        {
                         "name": "group112",
                         "children": [
                                        {"id":"288","link":"-","name":"Heidelberg platen press","decade":"1960s"}
                                ]
                        },
                        {
                         "name": "group113",
                         "children": [
                                        {"id":"223","link":"-","name":"Camera Praktica Super TL – shutter release","decade":"1960s"},
                                        {"id":"244","link":"-","name":"Mechanical calculator, Facit","decade":"1950s"}
                                ]
                        }
                        ]
                },

以下代码递归遍历树。如果paramchildren,则遍历其children。否则,它的 id 将附加到 results 数组,因此,在末尾 results 将包含叶子的 idgetResults returns results 以简化其用法。

var results = [];

function getResults(param) {
    if (!!param.children) {
        for (var child in param.children) {
            getResults(param.children[child]);
        }
    } else {
        results[results.length] = param.id;
    }
    return results;
}

试试这个方法。使用递归方法查找组,并使用另一种递归方法收集叶节点。

function getLeafNodes(leafNodes, obj){
    if(obj.children){
        obj.children.forEach(function(child){getLeafNodes(leafNodes,child)});
    } else{
        leafNodes.push(obj);
    }
}

function findIds(json,name){ 
   if(json.children){       
        if(json.name==name)  {
           var leafNodes = [];   
           getLeafNodes(leafNodes,json);
           console.log(leafNodes.map(function(leafNode){ return leafNode.id; })); //Logs leaf node ids to the console
        } else {
           json.children.forEach(function(child){                               
              findIds(child,name);
           });      
        }   
   }
}

执行下面的代码会打印出["014", "288", "223", "244"]

findIds(actualJSON,"group110");

这是一个通用的简洁递归答案,用于使用一些 jquery ($.map) 查找节点。 如果数据很深,请注意堆栈溢出! 此外,它不会继续在匹配节点内搜索更多匹配的子节点,因此它仅适用于搜索词未在逻辑上嵌套的情况。 这个方法利用了 $.map.

的数组扁平化特性
var found = (function walk(obj, searchKey, searchTerm) {
   if(!$.isPlainObject(obj)) return null; 
   return obj[searchKey] === searchTerm ? [obj] : $.map(obj, function (lev) { 
       return walk(lev, searchKey, searchTerm); 
   }); 
})(data, 'name', 'group110');

对此进行扩展以解决上述具体问题...

var found = (function walk(obj, searchTerm) { 
   if(!$.isPlainObject(obj)) return null;
   return obj.name == searchTerm 
            ? $.map(obj.children, function(c){ 
                  return $.map(c.children, function(f){ return f.id; }); })
            : $.map(obj.children, function (lev) { 
                  return walk(lev, searchTerm); }); 
})(data, 'group110');

或者说

var found = (function walk(obj, lambda, term) { 
   if(!($.isPlainObject(obj) || $.isArray(obj))) return null;
   return lambda.call(obj, term) 
            ? $.map(obj.children, function(c){ 
                  return $.map(c.children, function(f){ return f.id; }); })
            : $.map(obj.children, function (lev) { 
                  return walk(lev, searchTerm); }); 
})(data, function(a){ return this.name == a; }, 'group110');