Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

递归数组得到深度Deep

如下数据

let tagsNodes = [
    {
      tagsId: 'start',
      text: '开始',
      children: [
        {
          tagsId: 'filter',
          text: '会员筛选',
          children: [
            {
              branchType: 'yesBranch',
              tagsId: 'filter',
              text: '会员筛选',
              children: [
                {
                  branchType: 'yesBranch',
                  tagsId: 'filter',
                  text: '会员筛选',
                  children: [
                    {
                      branchType: 'yesBranch',
                      tagsId: 'sendMessage',
                      text: '发消息'
                    },
                    {
                      branchType: 'noBranch',
                      tagsId: 'removeTags',
                      text: '移除标签'
                    }
                  ]
                },
                {
                  branchType: 'noBranch',
                  tagsId: 'addTags',
                  text: '增加标签'
                }
              ]
            },
            {
              branchType: 'noBranch',
              tagsId: 'coupon',
              text: '优惠券'
            }
          ]

        }
      ]
    }
  ]

想递归得到深度deepLength 如下图
image.png

想要的数据 就是增加一个 deepLength

 tags = [
    {
      tagsId: 'start',
      text: '开始',
      children: [
        {
          tagsId: 'filter',
          text: '会员筛选',
          deepLength: 3,
          children: [
            {
              branchType: 'yesBranch',
              tagsId: 'filter',
              text: '会员筛选',
              deepLength: 2,
              children: [
                {
                  branchType: 'yesBranch',
                  tagsId: 'filter',
                  text: '会员筛选',
                  deepLength: 1,
                  children: [
                    {
                      branchType: 'yesBranch',
                      tagsId: 'sendMessage',
                      text: '发消息'
                    },
                    {
                      branchType: 'noBranch',
                      tagsId: 'removeTags',
                      text: '移除标签'
                    }
                  ]
                },
                {
                  branchType: 'noBranch',
                  tagsId: 'addTags',
                  text: '增加标签'
                }
              ]
            },
            {
              branchType: 'noBranch',
              tagsId: 'coupon',
              text: '优惠券'
            }
          ]

        }
      ]
    }
  ]

实现

  tagNodesDeepLength(val: TagNodes[], deepLength: number) {
    val.forEach((item) => {
      // 如果都有条件 则递归深度 + 1

      if (item.tagsId === 'filter' && (item.children && item.children.length === 2)) {
        deepLength += 1
        item.deepLength = deepLength
      }
      if (item.children && item.children.length) {
        this.tagNodesDeepLength(item.children, deepLength)
      }
    })
  }
  
      this.tagNodesDeepLength(val, 0)

这样 得到的数据 第一级是deepLength:1,第二级deepLength:2,第三级 deepLength:3, 和想要的不符 应该第一级是deepLength:3,第二级deepLength:2,第三级 deepLength:1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

一般递归都是先处理再递归,但你这个需要递归统计出来的数据,所以要先递归,后计算,再赋值。

function getDeep(node: TagNodes): number {
    if (!node.children?.length) {
        return node.deepLength = 1;
    }

    const lengthes = node.children
        .map(node => getDeep(node));

    return node.deepLength = Math.max(...lengthes) + 1;
}

function getDeepLengthes(nodes: TagNodes[]) {
    nodes.forEach(node => getDeep(node));
    return nodes;
}

这是从题上的数据算出来的结果,验证一下看对不对:

[
  {
    "tagsId": "start",
    "text": "开始",
    "deepLength": 5,
    "children": [
      {
        "tagsId": "filter",
        "text": "会员筛选",
        "deepLength": 4,
        "children": [
          {
            "branchType": "yesBranch",        
            "tagsId": "filter",
            "text": "会员筛选",
            "deepLength": 3,
            "children": [
              {
                "branchType": "yesBranch",    
                "tagsId": "filter",
                "text": "会员筛选",
                "deepLength": 2,
                "children": [
                  {
                    "branchType": "yesBranch",
                    "tagsId": "sendMessage",  
                    "text": "发消息",
                    "deepLength": 1
                  },
                  {
                    "branchType": "noBranch",
                    "tagsId": "removeTags",
                    "text": "移除标签",
                    "deepLength": 1
                  }
                ]
              },
              {
                "branchType": "noBranch",
                "tagsId": "addTags",
                "text": "增加标签",
                "deepLength": 1
              }
            ]
          },
          {
            "branchType": "noBranch",
            "tagsId": "coupon",
            "text": "优惠券",
            "deepLength": 1
          }
        ]
      }
    ]
  }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...