递归 js,递归改所有node里面属性名字

1.递归拿出不是叶子结点的id数组

getPriv(this.roleId).then((response) => {
             const privData = this.privData
             var a = []
             var forfn = function (list) {
               for (var i = 0; i < list.length; i++) {
                 if (list[i].children) {
                   a.push(list[i].id)
                   forfn(list[i].children)
                 }
               }
             }
             forfn(privData)
            //比对a和b this.privIds为在b数组中去除a数组包含的元素后形成的数组
             var b = response.data
             let RemoveSame = [...new Set([...a, ...b])]
             let SamePart = a.filter((item) => b.includes(item))
             let Difference = RemoveSame.filter(
               (item) => !SamePart.includes(item)
             )
             this.privIds = Difference
             // this.$refs.menu.setCheckedNodes(response.data)
           })

2.递归改所有node里面属性名字

getTree().then((response) => {
      this.menuOptions = []
      //data为需要修改的tree,这里主要是为antd design 里面select规范数据

      const ass = (data) => {
        let item = []
        data.map((list, i) => {
          let newData = {}
          newData.title = list.label
          newData.id = list.id
          newData.parentId = list.parentId
          newData.children = list.children ? ass(children) : [] //如果还有子集,就再次调用自己
          item.push(newData)
        })
        return item
      }
      var rda = ass(response.data)

      this.menuOptions = rda
    })

你可能感兴趣的:(javascript前端)