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

标签 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: []
  },
]

错误的函数,总是返回'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);
    }
}

正在进行深度搜索,但没有返回任何结果。您应该将结果分配给搜索,如果结果不是未定义,则返回它或立即中断 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>

关于javascript - 通过一些参数在 javascript 树中查找节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32095577/

相关文章:

Javascript 相当于 Excel 的 Text 函数

javascript - 达到值后解决递归 Promise

javascript - 使用 Backbone 和 Underscore 进行多属性查找

java递归测试arraylist是否相等

javascript - 递归地循环遍历树,并在迭代之间设置超时

javascript - 从具有指定日期键的数组中查找和删除对象

node.js - 如何在underscore js中结合使用after和each来创建同步循环

javascript - 我可以更改 key 对数据的存储方式以使访问非常高效吗?

javascript - 如何解码字符实体引用

javascript - 使用谷歌地图突出整条道路