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

时间:2015-08-19 12:29:58

标签: javascript recursion underscore.js

我尝试为下划线编写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: []
  },
]

错误的功能,alaways返回'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;
    }
  });

哪里出错?请帮帮我

1 个答案:

答案 0 :(得分:1)

在您的代码中,您的

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>
&#13;
&#13;
&#13;