账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    js 递归查询的问题
    21
    0

    我想在nodes这个数组可能有N层,我想在最后一层children的数组对象里面,每个对象加一些属性,如下

    nodes = [

    {
      title: 'parent 1',
      key: '100',
      children: [
        {
          title: 'parent 1-0',
          key: '1001',
          children: [
            { title: 'leaf', key: '10010' },
            { title: 'leaf', key: '10011' },
            { title: 'leaf', key: '10012' }
          ]
        },
      ]
    }

    ];

    目的:
    nodes = [

    {
      title: 'parent 1',
      key: '100',
      children: [
        {
          title: 'parent 1-0',
          key: '1001',
          children: [
            { title: 'leaf', key: '10010',test:'aaaa '},
            { title: 'leaf', key: '10011',test:'aaaa ' },
            { title: 'leaf', key: '10012',test:'aaaa '}
          ]
        },
      ]
    }

    ];

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 广目天王留博叉 普通会员 1楼

      递归查询是一种在JavaScript中实现复杂查询的方法。在JavaScript中,递归查询可以使用函数调用来实现,这样可以避免重复计算。

      以下是一个使用递归查询的简单示例:

      ```javascript function search(items, item, index) { if (index === items.length) { return true; } if (items[index] === item) { return search(items, item, index + 1); } else { return search(items, item, index + 1); } }

      let items = [1, 2, 3, 4, 5]; let item = 3; let result = search(items, item, 0); console.log(result); // 输出:true ```

      在这个例子中,search函数接受一个数组,一个要搜索的项,以及一个当前索引。如果当前索引等于数组长度,那么返回true,表示找到了要搜索的项。如果当前项等于要搜索的项,那么递归调用search函数,将当前索引加1。如果当前项不等于要搜索的项,那么递归调用search函数,将当前索引加1。

      在主程序中,我们定义了一个数组和一个要搜索的项,然后调用search函数。如果找到了要搜索的项,result变量将被设置为true。否则,result变量将被设置为false

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部