树状结构根据获取父级id

<template>
  <div>
    <el-button @click="clickBtn">点击</el-button>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      data: [{
        children: [{
          children: [{
            children: [{
              children: [{
                children: [],
                id: "173125778349756915",
                parentId: "173125778349756900",
                planName: "项目1-1-1-1-1",
              }],
              id: "173125778349756900",
              parentId: "173125778349756894",
              planName: "项目1-1-1-1",
            }],
            id: "173125778349756894",
            parentId: "173125778349756508",
            planName: "项目1-1-1",
          }],
          id: "173125778349756508",
          parentId: "173125778349756507",
          planName: "项目1-1",
        }],
        id: "173125778349756507",
        parentId: "0",
        planName: "项目1",
      }],
      arr: []
    };
  },
  filters: {},
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  beforeDestroy() {},
  methods: {
    clickBtn() {
      this.arr = []
      let newData = this.flatten(this.data);
      let arrData = []
      arrData = this.findKey("173125778349756915", newData)
      console.log(arrData.splice(0, arrData.length - 1));
    },
    flatten(arr) {
      var res = [];
      arr.map((item) => {
        if (Array.isArray(item.children)) {
          res = res.concat(this.flatten(item.children));
        }
        res.push(item);
      });
      return res;
    },
    findKey(key, data1) {
      if (key) {
        this.arr.push(key);
      }
      let index = data1.findIndex((item) => item.id === key);
      if (index >= 0) {
        this.findKey(data1[index].parentId, data1);
      }
      return this.arr;
    }
  }
}
</script>

<style scoped lang="less"></style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值