美文网首页
根据子节点得到父节点的数据

根据子节点得到父节点的数据

作者: Mark_ZSQ | 来源:发表于2019-11-25 17:14 被阅读0次
    tableData: [
            {
              id: 1,
              level0: 10001,
              title: 'appearance',
              salesStatus: 'ON',
              stickers: [],
              children: [
                {
                  id: 2,
                  level1: 10002,
                  stickers: [
                    {
                      title: 'line',
                      style: 'white',
                    },
                    {
                      title: 'bend',
                      style: 'black',
                    },
                    {
                      title: 'bend',
                      style: 'black',
                    },
                    {
                      title: 'bend',
                      style: 'black',
                    }
                  ],
                  icon: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=c8729bb7d81b0ef46ce89f58e5ff36e7/37d3d539b6003af356370ff93f2ac65c1138b69e.jpg',
                  title: 'appearance_hair',
                  salesStatus: 'ON',
                },
                {
                  id: 3,
                  level1: 10003,
                  stickers: [],
                  icon: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=2f40ba7c5e0fd9f9b41a5d3b4444bf4f/d1a20cf431adcbefb2b1eb06a3af2edda3cc9f2d.jpg',
                  title: 'appearance_eye',
                  salesStatus: 'ON',
                  children: [
                    {
                      id: 4,
                      level2: 10004,
                      stickers: [],
                      icon: 'https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=0139132c8801a18be4e61a1dff466c6d/b3fb43166d224f4aaa49e61306f790529922d1da.jpg',
                      title: 'appearance_shape',
                      salesStatus: 'ON',
                      children: [
                        {
                          id: 7,
                          level3: 10006,
                          stickers: [],
                          icon: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=2f40ba7c5e0fd9f9b41a5d3b4444bf4f/d1a20cf431adcbefb2b1eb06a3af2edda3cc9f2d.jpg',
                          title: 'eyelid_type',
                          salesStatus: 'ON',
                        }
                      ]
                    },
                    {
                      id: 5,
                      level2: 10005,
                      stickers: [],
                      icon: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=2f40ba7c5e0fd9f9b41a5d3b4444bf4f/d1a20cf431adcbefb2b1eb06a3af2edda3cc9f2d.jpg',
                      title: 'appearance_eyelid',
                      salesStatus: 'ON',
                      children: [
                        {
                          id: 6,
                          level3: 10006,
                          stickers: [],
                          icon: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=2f40ba7c5e0fd9f9b41a5d3b4444bf4f/d1a20cf431adcbefb2b1eb06a3af2edda3cc9f2d.jpg',
                          title: 'eyelid_type',
                          salesStatus: 'ON',
                        }
                      ]
                    }
                  ]
                },
                {
                  id: 9,
                  level1: 10007,
                  stickers: [
                    {
                      title: 'line',
                      style: 'white',
                    },
                    {
                      title: 'bend',
                      style: 'black',
                    },
                    {
                      title: 'bend',
                      style: 'black',
                    },
                    {
                      title: 'bend',
                      style: 'black',
                    }
                  ],
                  icon: 'https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=2f40ba7c5e0fd9f9b41a5d3b4444bf4f/d1a20cf431adcbefb2b1eb06a3af2edda3cc9f2d.jpg',
                  title: 'appearance_mouth',
                  salesStatus: 'OFF',
                },
              ]
            }
          ],
    
     console.log(this.treeFindPath(this.tableData, data=> data.title === 'appearance_hair'))
    
        treeFindPath (tree, func, path = []) {
          if (!tree) return []
          for (const data of tree) {
            // 这里按照你的需求来存放最后返回的内容
            path.push(data.title)
            if (func(data)) return path
            if (data.children) {
              const findChildren = this.treeFindPath(data.children, func, path)
              if (findChildren.length) return findChildren
            }
            path.pop()
          }
          return []
        }
    
    image.png

    相关文章

      网友评论

          本文标题:根据子节点得到父节点的数据

          本文链接:https://www.haomeiwen.com/subject/xoyjwctx.html