繁体   English   中英

通过一些参数在JavaScript树中查找节点

[英]Find node in javascript tree by some params

我尝试为下划线编写mixin,例如,可以通过一些参数找到节点:

_.findDeep(tree, {id: 5456, parent_id: 555})

树:

var tree = [
  {
    id: 22,
    name: 'qqqq',
    depth: 0,
    parent_id: 11,
    children: [
      {
        id: 222,
        name: 'ttttt',
        depth: 1,
        parent_id: 444,
        children: [],
        positions: []
      },
      {
        id: 5456,
        name: 'yyyy',
        depth: 1,
        parent_id: 555,
        children: [
          {
            id: 6767,
            name: 'dfgfdg',
            depth: 3,
            parent_id: 6564,
            children: [],
            positions: []
          },
          {
            id: 4345,
            name: 'dfgdgfg',
            depth: 3,
            parent_id: 45234,
            children: [],
            positions: []
          },
        ],
        positions: []
      },
    ],
    positions: [
      {
        id: 14,
        name: 'rere',
        price: 20
      },
      {
        id: 12,
        name: 'tttyty',
        price: 30
      },
    ]
  },
  {
    id: 33,
    name: 'wwww',
    depth: 0,
    parent_id: 22,
    children: [],
    positions: []
  },
  {
    id: 44,
    name: 'eeee',
    depth: 0,
    parent_id: 33,
    children: [],
    positions: []
  },
]

错误的函数总是返回“ undefined”,但console.log显示已建立的节点:

  _.mixin({
    findDeep: function(items, attrs) {
      var key, n_key, n_value, result, value;
      result = _.findWhere(items, attrs);
      console.log(items, result, _.isUndefined(result));
      if (_.isUndefined(result)) {
        for (key in items) {
          value = items[key];
          for (n_key in value) {
            n_value = value[n_key];
            if (_.isObject(n_value) || _.isArray(n_value)) {
              result = _.findDeep(n_value, attrs);
              if (!_.isUndefined(result)) {
                return result;
              }
            }
          }
        }
      }
      return result;
    }
  });

错误在哪里? 请帮我

在您的代码中,

for (n_key in value) {
    n_value = value[n_key];
    if (_.isObject(n_value) || _.isArray(n_value)) {
       _.findDeep(n_value, attrs);
    }
}

正在进行深度搜索,但不返回任何结果。 您应该将结果分配给搜索,如果结果不是undefined ,请将其返回或立即中断for循环。

这样就变成了:

_.mixin({
  findDeep: function(items, attrs) {
    var key, n_key, n_value, result, value;
    result = _.findWhere(items, attrs);
    console.log(items, result, _.isUndefined(result));
    if (_.isUndefined(result)) {
      for (key in items) {
        value = items[key];
        for (n_key in value) {
          n_value = value[n_key];
          if (_.isObject(n_value) || _.isArray(n_value)) {
            result = _.findDeep(n_value, attrs);
          }

          // Once you find the result, you can return the founded result
          if (!_.isUndefined(result)) {
            return result;
          }

        }
      }
    }
    return result;
  }
});

片段以显示正确的结果:

 var tree = [ { id: 22, name: 'qqqq', depth: 0, parent_id: 11, children: [ { id: 222, name: 'ttttt', depth: 1, parent_id: 444, children: [], positions: [] }, { id: 5456, name: 'yyyy', depth: 1, parent_id: 555, children: [ { id: 6767, name: 'dfgfdg', depth: 3, parent_id: 6564, children: [], positions: [] }, { id: 4345, name: 'dfgdgfg', depth: 3, parent_id: 45234, children: [], positions: [] }, ], positions: [] }, ], positions: [ { id: 14, name: 'rere', price: 20 }, { id: 12, name: 'tttyty', price: 30 }, ] }, { id: 33, name: 'wwww', depth: 0, parent_id: 22, children: [], positions: [] }, { id: 44, name: 'eeee', depth: 0, parent_id: 33, children: [], positions: [] }, ]; _.mixin({ findDeep: function(items, attrs) { var key, n_key, n_value, result, value; result = _.findWhere(items, attrs); console.log(items, result, _.isUndefined(result)); if (_.isUndefined(result)) { for (key in items) { value = items[key]; for (n_key in value) { n_value = value[n_key]; if (_.isObject(n_value) || _.isArray(n_value)) { result = _.findDeep(n_value, attrs); } // Once you find the result, you can return the founded result if (!_.isUndefined(result)) { return result; } } } } return result; } }); console.log(_.findDeep(tree, {id: 5456, parent_id: 555})); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM